Để tìm nạp một giá trị từ tài liệu lồng nhau, hãy sử dụng ký hiệu dấu chấm. Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> db.demo591.insert([
... { "Name": "John", "Age": 23 },
... {"Name": "Carol", "Age": 26},
... { "Name": "Robert", "Age": 29,
... details:[
... {
... Email:"Robert@gmail.com",CountryName:"US"},{"Post":35}
... ]}
... ]);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
}) 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.demo591.find();
Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd3"), "Name" : "John", "Age" : 23 }
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd4"), "Name" : "Carol", "Age" : 26 }
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd5"), "Name" : "Robert", "Age" : 29, "details" : [ {
"Email" : "Robert@gmail.com", "CountryName" : "US" }, { "Post" : 35 } ] } Sau đây là truy vấn để tìm nạp tài liệu lồng nhau bằng ký hiệu dấu chấm -
> db.demo591.find({"details.Email": "Robert@gmail.com"}); Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd5"), "Name" : "Robert", "Age" : 29, "details" : [
{ "Email" : "Robert@gmail.com", "CountryName" : "US" }, { "Post" : 35 }
] }