鉴于一个具有值的数组,如何找到匹配 MongoDB 查询的文档?
对于特定文档,请使用 MongoDB $in。让我们创建一个包含文档的集合 −
> db.demo511.insertOne({"ListOfProject":["Library Management System","Hospital Management System"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e88473a987b6e0e9d18f585") } > db.demo511.insertOne({"ListOfProject":["Online Web Tracking","Library Management System"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e884751987b6e0e9d18f586") } > db.demo511.insertOne({"ListOfProject":["Online Shopping Cart","Hospital Management System"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e884776987b6e0e9d18f587") }
借助 find() 方法显示集合中的所有文档 −
> db.demo511.find();
这将产生以下输出 −
{ "_id" : ObjectId("5e88473a987b6e0e9d18f585"), "ListOfProject" : [ "Library Management System", "Hospital Management System" ] } { "_id" : ObjectId("5e884751987b6e0e9d18f586"), "ListOfProject" : [ "Online Web Tracking", "Library Management System" ] } { "_id" : ObjectId("5e884776987b6e0e9d18f587"), "ListOfProject" : [ "Online Shopping Cart", "Hospital Management System" ] }
以下是对查询匹配文档的查询 −
> db.demo511.find({ "ListOfProject":{$in:["Hospital Management System","Online Shopping Cart"]}});
这将产生以下输出 −
{ "_id" : ObjectId("5e88473a987b6e0e9d18f585"), "ListOfProject" : [ "Library Management System", "Hospital Management System" ] } { "_id" : ObjectId("5e884776987b6e0e9d18f587"), "ListOfProject" : [ "Online Shopping Cart", "Hospital Management System" ] }
广告