11.Query an Array of Embedded Documents-官方文档摘录
总结
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:
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:
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:
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:
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:
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:
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:
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:
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:
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )
Additional Query Tutorials
For additional query examples, see:
11.Query an Array of Embedded Documents-官方文档摘录的更多相关文章
- 9.Query on Embedded/Nested Documents-官方文档摘录
1.插入案例 db.inventory.insertMany( [ { item: "journal", qty: 25, size: { h: 14, w: 21, uom: & ...
- Cocos Creator 加载和切换场景(官方文档摘录)
Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...
- ng的概念层次(官方文档摘录)
官方文档是这么说的: You write Angular applications by: composing HTML templates with Angularized markup, writ ...
- Cocos Creator 生命周期回调(官方文档摘录)
Cocos Creator 为组件脚本提供了生命周期的回调函数.用户通过定义特定的函数回调在特定的时期编写相关 脚本.目前提供给用户的声明周期回调函数有: onLoad start update la ...
- Cocos Creator 使用计时器(官方文档摘录)
在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...
- angular 模板语法(官方文档摘录)
https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...
- 8.Query Documents-官方文档摘录
总结 1 先插入数据 db.inventory.insertMany([ { item: "journal", qty: 25, size: { h: 14, w: 21, uom ...
- 10.Query an Array-官方文档摘录
1.插入 db.inventory.insertMany([ { item: "journal", qty: 25, tags: ["blank", " ...
- 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 ...
随机推荐
- redis源码学习_整数集合
redis里面的整数集合保存的都是整数,有int_16.int_32和int_64这3种类型,和C++中的set容器差不多. 同时具备如下特点: 1.set里面的数不重复,均为唯一. 2.set里面的 ...
- poj 1475 Pushing Boxes 推箱子(双bfs)
题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...
- python练习题3--for
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 程序分析: 假设该数为 x. 1.则:x + 100 = n2, x + 100 + 168 = m2 ...
- PHP文件包含
今天突然发现这个东西有好多奇怪的东西,特别写一下记一下 测试用的1.txt及phpinfo.php内容都是phpinfo() 截断: 好多.和好多./截断:这里不测试了,摘自代码审计一书,5.2可用, ...
- Java Drp项目实战——Web应用server
引言 Web应用server如今非常多人都在用,但是究竟什么是Web应用server呢,它与Webserver有什么关系,它与应用server又是什么关系,它是他们两种中的当中一种,还是简单的两种se ...
- js 数组取出最大值最小值和平均值的方法
1.直接遍历数组 ,,,,,,,]; ]; ;i<arr.length;i++){ if(max<arr[i]) max=arr[i]; } 2.借用Math的方法 ,,,,,,,]; v ...
- 【Mac + Appium + Python3.6学习(六)】之安装Android模拟器(Genymotion)并运行模拟器进行自动化
环境: MacOS:10.13.6 Virtualbox:5.2.22 Genymotion:genymotion-2.12.2 本文Virtualbox.Genymotion下载地址: 链接:htt ...
- 成功抓取csdn阅读量过万博文
http://images.cnblogs.com/cnblogs_com/elesos/1120632/o_111.png var commentscount = 1; 嵌套的评论算一条,这个可能有 ...
- 第二百二十七节,jQuery EasyUI,ComboTree(树型下拉框)组件
jQuery EasyUI,ComboTree(树型下拉框)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解EasyUI中ComboTree(树型下拉框)组件的使用方法,这个 ...
- mfc小工具开发之定时闹钟之---多线程急线程同步
一.MFC对多线程编程的支持 MFC中有两类线程,分别称之为工作者线程和用户界面线程.二者的主要区别在于工作者线程没有消息循环,而用户界面线程有自己的消息队列和消息循环. 工作者线程没有消息机制,通常 ...