9.Query on Embedded/Nested Documents-官方文档摘录
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:
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" }:
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:
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":
db.inventory.find( { "size.uom": "in" } )
Specify Match using Query Operator
A 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:
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":
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-官方文档摘录的更多相关文章
- Cocos Creator 加载和切换场景(官方文档摘录)
Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...
- ng的概念层次(官方文档摘录)
官方文档是这么说的: You write Angular applications by: composing HTML templates with Angularized markup, writ ...
- 13.Query for Null or Missing Fields-官方文档摘录
1 插入数据 db.inventory.insertMany([ { _id: 1, item: null }, { _id: 2 } ]) 2 查询null值 db.inventory.find({ ...
- 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 ...
- 11.Query an Array of Embedded Documents-官方文档摘录
总结 1.插入数据 db.inventory.insertMany( [ { item: "journal", instock: [ { warehouse: "A&qu ...
- 10.Query an Array-官方文档摘录
1.插入 db.inventory.insertMany([ { item: "journal", qty: 25, tags: ["blank", " ...
- 8.Query Documents-官方文档摘录
总结 1 先插入数据 db.inventory.insertMany([ { item: "journal", qty: 25, size: { h: 14, w: 21, uom ...
- 5.MongoDB CRUD Operations-官方文档摘录
总结 1. CRUD:create, read, update, and delete DOCUMENT 2.在3.2版本的插入方式 db.collection.insertOne() db.coll ...
随机推荐
- iOS之设置用户头像的圆角
1. 显示用户头像用UIImageView实现,添加默认图片后效果如下图所示,头像显示为矩形图片. 代码实现: // ViewController.m // SetUserImage // // Cr ...
- ORACLE / PLSQL 插入或更新数据的几种方式
第一种,直接用values 插入 ---日期需要用TO_DATE操作 INSERT INTO EMP (EMPNO,ENAME ,JOB,MGR, HIREDATE , SAL ,COMM, DEPT ...
- mysql循环查询
begin declare i int;set i = 1;lp1 : LOOPSELECT insert(t.bb,4,6,'XXXX') FROM t_aa t; set i = i+1; if ...
- PHP文件包含
今天突然发现这个东西有好多奇怪的东西,特别写一下记一下 测试用的1.txt及phpinfo.php内容都是phpinfo() 截断: 好多.和好多./截断:这里不测试了,摘自代码审计一书,5.2可用, ...
- npm太慢, 淘宝npm镜像使用方法[转]
淘宝 npm 地址: http://npm.taobao.org/ 如何使用 有很多方法来配置npm的registry地址,下面根据不同情境列出几种比较常用的方法.以淘宝npm镜像举例: 1.临时使用 ...
- css3兼容代码
1. 渐变:.gradient{ width:300px; height:150px; filter:alpha(opacity=100 finishopacity=50 style=1 startx ...
- C语言 百炼成钢26
/* 题目62: 有一下特征字符串"eerrrrqqAB33333ABa333333ABjsfdsfdsa" 编写一个业务函数, 实现功能1:实现按照子串"AB" ...
- C语言 函数指针二(正向调用)
//函数指针做函数参数 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<Wi ...
- 什么是 GOF(四人帮,全拼 Gang of Four)?
在 1994 年,由 Erich Gamma.Richard Helm.Ralph Johnson 和 John Vlissides 四人合著出版了一本名为 Design Patterns - Ele ...
- 详略。。设计模式2——单例变形(多例).。。。studying
★ 缓存在单例中的使用("单例+缓存"技术) 缓存在编程中使用非常频繁,有着非常关键的数据,它可以帮助程序实现以空间换取时间, 通常被设计成整个应用程序所共享的一个空间,现要求实现 ...