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 inventory
collection. 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 uom
equals "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 ...
随机推荐
- ado连接sql server
//ado连接sql server //头文件加上以下这句. #import "C:\Windows\system\msado15.dll" no_namespace rename ...
- phpMyAdmin安装教程
phpMyAdmin安装教程: 解压:将下载文件解压缩到 WEB 访问路径下.文件目录如phpmyadmin. 配置文件:然后配置目录下libraries文件下的 config.default.php ...
- python学习之time模块
time.time() 将时间作为浮点数返回. 在Windows和大多数Unix系统上,时代是1970年1月1日00:00:00(UTC),并且闰秒不计入从时代开始的秒数. >>> ...
- 5.3日,7:20开始 阮一峰js的早课学习
Infinity - Infinity // NaN Infinity / Infinity // NaN Infinity + Infinity // Infinity Infinity * Inf ...
- Qt 槽函数的使用
今天在代码中遇到这样一个问题,自己感觉槽和函数都写的没错,但是就是不执行槽函数,因为是一个定时器的使用,即定时时间到了就执行槽函数. SeventhWizardPage::SeventhWizardP ...
- JavaScript学习日志(1)
javascript用法: 1.HTML中的脚本必须位于<script>与</script>标签之间,可被放置在HTML页面的<body>和<head> ...
- Github使用之git回退到某个历史版本
1. 查找历史版本 使用git log命令查看所有的历史版本,获取你git的某个历史版本的id 假设查到历史版本的id是fae6966548e3ae76cfa7f38a461c438cf75ba965 ...
- Android Intent 用法全面总结(转载)
1. [代码]调用拨号程序 1 2 3 4 // 给移动客服10086拨打电话 Uri uri = Uri.parse("tel:10086"); Intent intent = ...
- hdu 5078
Osu! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Sub ...
- this,你是谁?
在js中this不像其它语言那样容易理解,它有时候指window对象,有时候又是其它对象,那么this,你到底是谁呢?要分析this就要先理解js中的方法定义,因为this一般都是在方法中使用的,而且 ...