Để tìm tài liệu với giá trị lớn hơn ngưỡng trong tài liệu lồng sâu, sử dụng ký pháp dấu chấm (dot notation) truy cập trường lồng nhau và đặt điều kiện với $gt.
Tạo collection với cấu trúc lồng sâu: information.id, information.details[].otherDetails.values:
> db.demo688.insert(
... {
... information:{id:1,details:[
... {otherDetails:{ values:75 } }
... ]}
... }
... )
WriteResult({ "nInserted" : 1 })
> db.demo688.insert({
... information:
... {
... id:2,
... details:
... [
... {otherDetails:{ values:78 }}
... ]
... }
... }
... )
WriteResult({ "nInserted" : 1 })
Hiển thị tất cả:
> db.demo688.find();
Kết quả:
{ "_id" : ObjectId("5ea57986a7e81adc6a0b3965"), "information" : { "id" : 1, "details" : [ { "otherDetails" : { "values" : 75 } } ] } }
{ "_id" : ObjectId("5ea5799ca7e81adc6a0b3966"), "information" : { "id" : 2, "details" : [ { "otherDetails" : { "values" : 78 } } ] } }
Truy vấn tìm values > 75 (dot notation: information.details.otherDetails.values):
> db.demo688.find({"information.details.otherDetails.values":{$gt:75}});
Kết quả (chỉ tài liệu id=2, values=78):
{ "_id" : ObjectId("5ea5799ca7e81adc6a0b3966"), "information" : { "id" : 2, "details" : [ { "otherDetails" : { "values" : 78 } } ] } }