Bạn có thể sử dụng toán tử $ in. Trước tiên, hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> 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")
} Sau đây là truy vấn để hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -
> db.tagCountDemo.find().pretty();
Điều này sẽ tạo ra kết quả sau -
{
"_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"
]
} Sau đây là truy vấn để nhận số lượng thẻ trong MongoDB
> db.tagCountDemo.find({"ListOfNames":{$in:['John','Carol']}}); Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5cd64b387924bb85b3f48944"), "ListOfNames" : [ "John", "Sam", "Carol" ] }
{ "_id" : ObjectId("5cd64b4b7924bb85b3f48945"), "ListOfNames" : [ "Bob", "David", "John" ] }
{ "_id" : ObjectId("5cd64b717924bb85b3f48947"), "ListOfNames" : [ "James", "Carol", "Jace" ] }