您可以使用$in运算符。首先让我们创建一个包含文档的集合-
> db.tagCountDemo.insertOne({"ListOfNames":["John","Sam","Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b387924bb85b3f48944") } > db.tagCountDemo.insertOne({"ListOfNames":["Bob","David","John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b4b7924bb85b3f48945") } > db.tagCountDemo.insertOne({"ListOfNames":["Mike","Robert","Chris"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b5d7924bb85b3f48946") } > db.tagCountDemo.insertOne({"ListOfNames":["James","Carol","Jace"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b717924bb85b3f48947") }
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.tagCountDemo.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5cd64b387924bb85b3f48944"), "ListOfNames" : [ "John", "Sam", "Carol" ] } { "_id" : ObjectId("5cd64b4b7924bb85b3f48945"), "ListOfNames" : [ "Bob", "David", "John" ] } { "_id" : ObjectId("5cd64b5d7924bb85b3f48946"), "ListOfNames" : [ "Mike", "Robert", "Chris" ] } { "_id" : ObjectId("5cd64b717924bb85b3f48947"), "ListOfNames" : [ "James", "Carol", "Jace" ] }
以下是在MongoDB中获取标签计数的查询
> db.tagCountDemo.find({"ListOfNames":{$in:['John','Carol']}});
这将产生以下输出-
{ "_id" : ObjectId("5cd64b387924bb85b3f48944"), "ListOfNames" : [ "John", "Sam", "Carol" ] } { "_id" : ObjectId("5cd64b4b7924bb85b3f48945"), "ListOfNames" : [ "Bob", "David", "John" ] } { "_id" : ObjectId("5cd64b717924bb85b3f48947"), "ListOfNames" : [ "James", "Carol", "Jace" ] }