mongodb查询嵌入/嵌套文档

这里演示如何使用:db.collection.find()方法对嵌入/嵌套文档的查询操作的示例。 此页面上的示例使用inventory集合。要填充库存(inventory)集合以准备一些数据,请运行以下命令:

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" }
]);

Shell

匹配嵌入/嵌套文档

要在作为嵌入/嵌套文档的字段上指定相等条件,请使用查询过滤器文档{<field>:<value>},其中<value>是要匹配的文档。

例如,以下查询选择字段size等于{ h: 14, w: 21, uom: "cm" }的所有文档:

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

Shell

整个嵌入式文档中的相等匹配需要精确匹配指定的<value>文档,包括字段顺序。
例如,以下查询与库存(inventory)集合中的任何文档不匹配:

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

Shell

查询嵌套字段

要在嵌入/嵌套文档中的字段上指定查询条件,请使用点符号(“field.nestedField”)。

在嵌套字段上指定等于匹配

以下示例选择在size字段中嵌套的字段uom等于“in”的所有文档:

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

nodejs查询嵌套字段.project(xxx) 

find.skip((page-1)*pageNum).sort(sort).limit(pageNum).project(whereN)

猜你喜欢

转载自blog.csdn.net/qq_24745557/article/details/81740909
今日推荐