1.插入案例

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

2.匹配内嵌文档

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

如果顺序不一致,则无法出正确数据

3 在嵌套字段中查找

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

db.inventory.find({"size.h":{$lt:15}})

db.inventory.find({
"size.h":{$lt:15},
"size.uom":"in",
status:"D"
})

This page provides examples of query operations on embedded/nested documents using thedb.collection.find() method in the mongo shell. The examples on this page use the inventorycollection. To populate the inventory collection, run the following:

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

You can run the operation in the web shell below:

Match an Embedded/Nested Document

To specify an equality condition on a field that is an embedded/nested document, use the query filter document { <field>: <value> } where <value> is the document to match.

For example, the following query selects all documents where the field size equals the document { h: 14,w: 21, uom: "cm" }:

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

Equality matches on the whole embedded document require an exact match of the specified <value>document, including the field order. For example, the following query does not match any documents in theinventory collection:

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

Query on Nested Field

To specify a query condition on fields in an embedded/nested document, use the dot notation("field.nestedField").

Specify Equality Match on a Nested Field

The following example selects all documents where the field uom nested in the size field equals "in":

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

Specify Match using Query Operator

query filter document can use the query operators to specify conditions in the following form:

{ <field1>: { <operator1>: <value1> }, ... }

The following query uses the less than operator ($lt) on the field h embedded in the size field:

Copy
db.inventory.find( { "size.h": { $lt: 15 } } )

Specify AND Condition

The following query selects all documents where the nested field h is less than 15, the nested field uomequals "in", and the status field equals "D":

Copy
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )

Additional Query Tutorials

For additional query examples, see:

最新文章

  1. 深入浅出 - Android系统移植与平台开发(五)- 定制手机模拟器ROM
  2. 记录Java的垃圾回收机制和几种引用
  3. POJ1947 Rebuilding Roads
  4. JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
  5. Delphi XE5教程11:Tokens
  6. NSSpeechSynthesizer 文字变语音
  7. 【javascript】复制到剪贴板功能(支持目前各种浏览器)
  8. phpstorm使用技巧
  9. return break continue 的区别
  10. 关于playframework2.5
  11. .Net程序员学用Oracle系列(12):增删改查
  12. HashPayloadPcapReader
  13. Python3实现ICMP远控后门(上)
  14. 【转】java中PriorityQueue优先级队列使用方法
  15. 关于freemarker 空变量的接收以及类型转换 笔记
  16. Luogu P1447 [NOI2010]能量采集
  17. 将一个字符串中的空格替换成“%20”(C、Python)
  18. Python之内置函数一
  19. CMS (内容管理系统)
  20. mysql5.7服务器The innodb_system data file &#39;ibdata1&#39; must be writable导致无法启动服务器

热门文章

  1. java强行删除文件(针对进程正在使用的文件的删除)
  2. JS高程3:DOM-节点层次
  3. IntelliJ IDEA Mybatis Plugin 破解安装
  4. (转)Python开发规范
  5. binutils工具集之---objdump
  6. libubox-blob/blobmsg
  7. CentOS/Linux 网卡设置 IP地址配置永久生效
  8. linux split
  9. 关于JSON日期格式显示及My97日期控件
  10. 从头认识java-17.4 具体解释同步(2)-具体解释竞争条件