MongoDB (10)-Query nested documents

Insert test data

db.inventory.insertMany( [   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }]);

The following chestnuts will use the test data here

Exactly match nested documents

> db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } ){ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }

This way of writing not only requires that the fields and values ​​of the nested documents are the same, but the order of the fields must also be the same

db.inventory.find(  { size: { w: 21, h: 14, uom: "cm" } }  )

The same document cannot be found if the order of the fields is changed

Query nested fields

To specify query conditions on fields in embedded/nested documents, the syntax format is as follows

"field.nestedField"

Same as JSON value, use  . To  find the next level field

Chestnut 1: Nested fields are equal to operations

> db.inventory.find( { "size.uom": "in" } ){ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9e"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" } 

The size field value is a document, find all documents where the h field value of the nested document is equal to in

Chestnut 2: Nested fields combined with a single query condition operator

> db.inventory.find( { "size.h": { $lt: 15 } } ){ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9d"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9e"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("60b5e622dd6e93ee8bf35aa1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

The size field value is a document, find all documents whose h field value of nested documents is less than 15

Chestnut 3: Combine and operation of multiple different nested fields

> db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } ){ "_id" : ObjectId("60b5e622dd6e93ee8bf35a9f"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }

Three conditions must be met at the same time

  • The size field, the h field value of the nested document is less than 15
  • The size field, the uom field value of the nested document is equal to in
  • The status field value is equal to D

Focus

If you specify a single field, you don’t need to add "" (of course, it doesn’t hurt to add it), such as

db.inventory.find( { status: "D" } )

If you specify a nested field, you must add "", such as the chestnut above

db.inventory.find( { "size.uom": "in" } )