MongoDB查询以给定具有值的数组查找匹配的文档?
> 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" ] }