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:

9.Query on Embedded/Nested Documents-官方文档摘录的更多相关文章

  1. Cocos Creator 加载和切换场景(官方文档摘录)

    Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...

  2. ng的概念层次(官方文档摘录)

    官方文档是这么说的: You write Angular applications by: composing HTML templates with Angularized markup, writ ...

  3. 13.Query for Null or Missing Fields-官方文档摘录

    1 插入数据 db.inventory.insertMany([ { _id: 1, item: null }, { _id: 2 } ]) 2 查询null值 db.inventory.find({ ...

  4. Cocos Creator 生命周期回调(官方文档摘录)

    Cocos Creator 为组件脚本提供了生命周期的回调函数.用户通过定义特定的函数回调在特定的时期编写相关 脚本.目前提供给用户的声明周期回调函数有: onLoad start update la ...

  5. Cocos Creator 使用计时器(官方文档摘录)

    在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...

  6. angular 模板语法(官方文档摘录)

    https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...

  7. 11.Query an Array of Embedded Documents-官方文档摘录

    总结 1.插入数据 db.inventory.insertMany( [ { item: "journal", instock: [ { warehouse: "A&qu ...

  8. 10.Query an Array-官方文档摘录

    1.插入 db.inventory.insertMany([ { item: "journal", qty: 25, tags: ["blank", " ...

  9. 8.Query Documents-官方文档摘录

    总结 1 先插入数据 db.inventory.insertMany([ { item: "journal", qty: 25, size: { h: 14, w: 21, uom ...

  10. 5.MongoDB CRUD Operations-官方文档摘录

    总结 1. CRUD:create, read, update, and delete DOCUMENT 2.在3.2版本的插入方式 db.collection.insertOne() db.coll ...

随机推荐

  1. [k8s]监控

    监控架构 参考 https://github.com/DataDog/the-monitor/blob/master/kubernetes/how-to-collect-and-graph-kuber ...

  2. Haskell示例

    i :: Int i = --add, sub :: Int -> Int -> Int add, sub :: (Num a) => a -> a -> a add a ...

  3. uGUI Anchor

    Anchor定位:inspector面板的Rect Transform组件中PosX左边的方框图标就是设置锚点的,做界面自适应时可定位控件在视图中的位置,与NGUI类似.Anchor+Canvas的C ...

  4. Django 图片路径问题

    在 django 中不像PHP那样有根目录的概念 而取而代之的是包的概念, 通过URLS.PY 来提供每个URL 对应的DJANGO的 函数来显示页面 在包的 temolates目录中 的html页面 ...

  5. STM32F10x_SPI(硬件接口 + 软件模拟)读写Flash(25Q16)

    推荐 分享一个大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来! http://www.captainbed.net/strongerhuang Ⅰ.写在前 ...

  6. Golang并发原理及GPM调度策略(一)

    其实从一开始了解到go的goroutine概念就应该想到,其实go应该就是在内核级线程的基础上做了一层逻辑上的虚拟线程(用户级线程)+ 线程调度系统,如此分析以后,goroutine也就不再那么神秘了 ...

  7. Nginx + PHP-FPM + MySQL + phpMyAdmin on Ubuntu (aliyun)

    今天抽空在阿里云上部署安装了PHP的环境 主要有nginx, php5 php-fpm mysql phpmyadmin 本文来源于:http://www.lonelycoder.be/nginx-p ...

  8. Nginx中修改php.ini的上传设置upload_max_filesize的值

    普遍的网络越来越快,以前小家子气的2M上传限制慢慢变得不合时宜了.最近就把2M的限制直接提升到了20M...代码层面很快就修改好了,没什么可说的.但是上线的话还得修改一下服务器的配置.服务器是Ngin ...

  9. splay旋转模板

    splay旋转模板 void rotate(int x) { int y=f[x],z=son(x);f[x]=f[y]; if (f[x]) t[f[x]][son(y)]=x; t[y][z]=t ...

  10. Github使用之git回退到某个历史版本

    1. 查找历史版本 使用git log命令查看所有的历史版本,获取你git的某个历史版本的id 假设查到历史版本的id是fae6966548e3ae76cfa7f38a461c438cf75ba965 ...