Đối với điều này, hãy sử dụng toán tử $ being. 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 -
>dbfindDocumentContainsSpecificFieldDemoinsertOne({"ProductPrices":{"Product1":10,"Pr oduct2":50}});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cf2385bb64a577be5a2bc14")
}
>dbfindDocumentContainsSpecificFieldDemoinsertOne({"ProductPrices":{"Product3":150,"P roduct7":100,"Product5":250}});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cf2387eb64a577be5a2bc15")
} 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 () -
> dbfindDocumentContainsSpecificFieldDemofind()pretty();
Điều này sẽ tạo ra tài liệu sau -
{
"_id" : ObjectId("5cf2385bb64a577be5a2bc14"),
"ProductPrices" : {
"Product1" : 10,
"Product2" : 50
}
}
{
"_id" : ObjectId("5cf2387eb64a577be5a2bc15"),
"ProductPrices" : {
"Product3" : 150,
"Product7" : 100,
"Product5" : 250
}
} Sau đây là truy vấn để tìm tài liệu có chứa trường cụ thể -
> dbfindDocumentContainsSpecificFieldDemofind({"ProductPricesProduct2":{$exists:true}}); Điều này sẽ tạo ra tài liệu sau -
{ "_id" : ObjectId("5cf2385bb64a577be5a2bc14"), "ProductPrices" : { "Product1" : 10, "Product2" : 50 } }