总结

1.插入数据

db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);

2.查找嵌套在数组中的文档

db.inventory.find({"instock":{"warehouse":"A","qty":5}})

3.根据数组索引查找内嵌文档元素

db.inventory.find({"instock.0.qty":{$lte:20}})

如果不是精确到具体的索引元素,则只要里面满足即可展现

db.inventory.find({"instock.qty":{$lte:20}})

4.使用elemMatch来查找符合数组元素的条件

db.inventory.find({"instock":{$elemMatch:{qty:5,warehouse:"A"}}})

5.使用elemMatch查找范围

db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )

This page provides examples of query operations on an array of 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", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);

You can run the operation in the web shell below:

Query for a Document Nested in an Array

The following examples selects all documents where an element in the instock array matches the specified document:

Copy
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )

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

Copy
db.inventory.find( { "instock": { qty: 5, warehouse: "A" } } )

Specify a Query Condition on a Field in an Array of Documents

Use the Array Index to Query for a Field in the Embedded Document

Using the dot notation, you can specify query conditions for field in a document at a particular index or position of the array. The array uses zero-based indexing.

The following example selects all documents where the instock array has as its first element a document that contains the field qty whose value is less than or equal to 20:

Copy
db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )

Specify a Query Condition on a Field Embedded in an Array of Documents

If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot (.) and the name of the field in the nested document.

The following example selects all documents where the instock array has at least one embedded document that contains the field qty whose value is less than or equal to 20:

Copy
db.inventory.find( { 'instock.qty': { $lte: 20 } } )

Specify Multiple Conditions for Array of Documents

When specifying conditions on more than one field nested in an array of documents, you can specify the query such that either a single document meets these condition or any combination of documents (including a single document) in the array meets the conditions.

A Single Nested Document Meets Multiple Query Conditions on Nested Fields

Use $elemMatch operator to specify multiple criteria on an array of embedded documents such that at least one embedded document satisfies all the specified criteria.

The following example queries for documents where the instock array has at least one embedded document that contains both the field qty equal to 5 and the field warehouse equal to A:

Copy
db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )

The following example queries for documents where the instock array has at least one embedded document that contains the field qty that is greater than 10 and less than or equal to 20:

Copy
db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )

Combination of Elements Satisfies the Criteria

If the compound query conditions on an array field do not use the $elemMatch operator, the query selects those documents whose array contains any combination of elements that satisfies the conditions.

For example, the following query matches documents where any document nested in the instock array has the qty field greater than 10 and any document (but not necessarily the same embedded document) in the array has the qty field less than or equal to 20:

Copy
db.inventory.find( { "instock.qty": { $gt: 10,  $lte: 20 } } )

The following example queries for documents where the instock array has at least one embedded document that contains the field qty equal to 5 and at least one embedded document (but not necessarily the same embedded document) that contains the field warehouse equal to A:

Copy
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )

Additional Query Tutorials

For additional query examples, see:

11.Query an Array of Embedded Documents-官方文档摘录的更多相关文章

  1. 9.Query on Embedded/Nested Documents-官方文档摘录

    1.插入案例 db.inventory.insertMany( [ { item: "journal", qty: 25, size: { h: 14, w: 21, uom: & ...

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

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

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

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

  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. 8.Query Documents-官方文档摘录

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

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

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

  9. Gora官方文档之二:Gora对Map-Reduce的支持 分类: C_OHTERS 2015-01-31 11:27 232人阅读 评论(0) 收藏

    参考官方文档:http://gora.apache.org/current/tutorial.html 项目代码见:https://code.csdn.net/jediael_lu/mygoradem ...

随机推荐

  1. (2.0)Smali系列学习之Smali语法

    一.smali的包中信息 .class  public Lcom/aaaaa;.super  Lcom/bbbbb;.source "ccccc.java" 1.它是com.aaa ...

  2. ld -l选项注意事项

    在程序中用到某个静态库,使用命令: gcc bin -llibrary.a object.o 结果发现找不到library.a中的某些函数符号 undefine reference to ... 通过 ...

  3. CodeForces 584D Dima and Lisa

    1e9 以内的判断一个数是否是素数,可以直接朴素的暴力.   这倒题除了考虑1e9以内的素数的判断,还有一个歌德巴赫猜想:任意一个奇数都可一分解为三个素数的和. 第三个结论:素数是密集的,1e9以内, ...

  4. 基于ffmpeg 直播推流和播放rtmp (IOS源码)

    ios直播推流每秒能达到30帧,比安卓要强,视频采用软编码的话手机会发烫,得采用码编码,播放视频采用opengl渲染. ffmpeg初始化代码如下: int init_Code(int width, ...

  5. Yii2 Restful API 原理分析

    Yii2 有个很重要的特性是对 Restful API的默认支持, 通过短短的几个配置就可以实现简单的对现有Model的RESTful API 参考另一篇文章: http://www.cnblogs. ...

  6. phpcms 初次建站心得

    最近要给客户建个网站,考虑到效率问题,直接找了个开源的phpcms,(现在被收购了,以前的时候我还知道是个开源的).由于对这个东西不熟悉,原来就是了解一些,php的建站系统,php的MVC框架.故此, ...

  7. VIM配置入门

    原文链接: http://www.ruanyifeng.com/blog/2018/09/vimrc.html 个人增加了两张收集来的图.

  8. C#反射实例(一) 利用反射使用类库

    在网上查找了不少的资料,可以说大同小异,概念性的东西网上一搜一堆,今天把反射的东西整理了一下,供大家使用,我保证我这里是最全面的东西,当然也是基础的东西,在学好了这一切的基础上,大家可以学习反射的具体 ...

  9. datagrid使用要点

    table自适应: (fit:true(设置table)) 列自动撑开:fitColumns: true,注意给列的width属性赋值

  10. 关于OBJC

    http://www.objc.io/ objc这个站点是:关于objective-c语言的最佳实践和高阶技术的期刊. 看了几期非常不错,所以计划每天抽出时间翻译一篇文章和大家一起分享.