Để tìm tài liệu theo tên trường với một giá trị cụ thể, bạn có thể sử dụng toán tử $ being. Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu
> db.findByFieldName.insertOne( { "Client":{ "ClientDetails":{ "ClientName":"Larry", "ClientAge":29 }, "ClientProjectDetails":{ "ProjectName":"Online Book Store", "TeamSize":10, "TechnologyUsed":"Spring Boot" } } } );
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9e93b2d628fa4220163b64")
}
> db.findByFieldName.insertOne({
... " Client":{
... " ClientDetails":{
... " ClientName":"Chris",
... " ClientAge":27
... },
... "ClientEducationDetails":{
... " isEducated":true,
... "CollegeName":"M.I.T."
...
... }
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9e9421d628fa4220163b65")
} 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.findByFieldName.find().pretty();
Điều này sẽ tạo ra kết quả sau
{
"_id" : ObjectId("5c9e93b2d628fa4220163b64"),
"Client" : {
"ClientDetails" : {
"ClientName" : "Larry",
"ClientAge" : 29
},
"ClientProjectDetails" : {
"ProjectName" : "Online Book Store",
"TeamSize" : 10,
"TechnologyUsed" : "Spring Boot"
}
}
}
{
"_id" : ObjectId("5c9e9421d628fa4220163b65"),
"Client" : {
"ClientDetails" : {
"ClientName" : "Chris",
"ClientAge" : 27
},
"ClientEducationDetails" : {
"isEducated" : true,
"CollegeName" : "M.I.T."
}
}
} Sau đây là truy vấn để tìm tài liệu theo tên trường
> db.findByFieldName.find({"Client.ClientProjectDetails":{$exists: true}}).pretty(); Điều này sẽ tạo ra kết quả sau
{
"_id" : ObjectId("5c9e93b2d628fa4220163b64"),
"Client" : {
"ClientDetails" : {
"ClientName" : "Larry",
"ClientAge" : 29
},
"ClientProjectDetails" : {
"ProjectName" : "Online Book Store",
"TeamSize" : 10,
"TechnologyUsed" : "Spring Boot"
}
}
}