12.Project Fields to Return from Query-官方文档摘录
1 插入例句
db.inventory.insertMany( [
{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
2.只显示需要显示的部分
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )

将ID列也省去
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )

3.查找内嵌文档
db.inventory.find(
{status:"A"},
{item:1,status:1,"size.uom":1} )

4.对数组的查找
db.inventory.find( { status: "A" }, { name: 1, status: 1, instock: { $slice: -1 },_id:0 } )

By default, queries in MongoDB return all fields in matching documents. To limit the amount of data that MongoDB sends to applications, you can include a projection document to specify or restrict fields to return.
This page provides examples of query operations with projection using the db.collection.find()method in the mongo shell. The examples on this page use the inventory collection. To populate theinventory collection, run the following:
db.inventory.insertMany( [
{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
You can run the operation in the web shell below:
Return All Fields in Matching Documents
If you do not specify a projection document, the db.collection.find() method returns all fields in the matching documents.
The following example returns all fields from all documents in the inventory collection where the statusequals "A":
db.inventory.find( { status: "A" } )
The operation corresponds to the following SQL statement:
SELECT * from inventory WHERE status = "A"
Return the Specified Fields and the _id Field Only
A projection can explicitly include several fields by setting the <field> to 1 in the projection document. The following operation returns all documents that match the query. In the result set, only the item, status and, by default, the _id fields return in the matching documents.
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
The operation corresponds to the following SQL statement:
SELECT _id, item, status from inventory WHERE status = "A"
Suppress _id Field
You can remove the _id field from the results by setting its exclusion <field> to 0 in the projection, as in the following example:
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
The operation corresponds to the following SQL statement:
SELECT item, status from inventory WHERE status = "A"
Return All But the Excluded Fields
Instead of listing the fields to return in the matching document, you can use a projection to exclude specific fields. The following example which returns all fields except for the status and the instock fields in the matching documents:
db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
With the exception of the _id field, you cannot combine inclusion and exclusion statements in projection documents.
Return Specific Fields in Embedded Documents
You can return specific fields in an embedded document. Use the dot notation to refer to the embedded field and set to 1 in the projection document.
The following example returns: the _id field (returned by default), item field, status field, and the uom field in the size document; the uom field remains embedded in the size document.
db.inventory.find(
{ status: "A" },
{ item: 1, status: 1, "size.uom": 1 }
)
Suppress Specific Fields in Embedded Documents
You can suppress specific fields in an embedded document. Use the dot notation to refer to the embedded field in the projection document and set to 0.
The following example specifies a projection to exclude the uom field inside the size document. All other fields are returned in the matching documents:
db.inventory.find(
{ status: "A" },
{ "size.uom": 0 }
)
Projection on Embedded Documents in an Array
Use dot notation to project specific fields inside documents embedded in an array.
The following example specifies a projection to return the item field, the status field, and the qty field in the documents embedded in the instock array. The _id field is returned by default.
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )
Project Specific Array Elements in the Returned Array
For fields that contain arrays, MongoDB provides the following projection operators: $elemMatch, $slice, and $.
The following example uses the $slice projection operator to return just the last element in the instockarray.
db.inventory.find( { status: "A" }, { name: 1, status: 1, instock: { $slice: -1 } } )
$elemMatch, $slice, and $ are the only way to project specific elements to include in the returned array. For instance, you cannot project specific array elements using the array index; e.g. { "instock.0": 1 }projection will not project the array with the first element.
SEE ALSO
12.Project Fields to Return from Query-官方文档摘录的更多相关文章
- 8.Query 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 ...
- MongoDB - MongoDB CRUD Operations, Query Documents, Project Fields to Return from Query
By default, queries in MongoDB return all fields in matching documents. To limit the amount of data ...
- 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 ...
- 20191106 Spring Boot官方文档学习(1-2)
学习内容相关信息 最新版本:2.2.0 CURRENT GA 官网地址 官方文档地址 单页版文档地址 代码生成网址 2.入门 Spring Boot的主要目标是: 为所有Spring开发提供更快且入门 ...
- Spring 4 官方文档学习(十二)View技术
关键词:view technology.template.template engine.markup.内容较多,按需查用即可. 介绍 Thymeleaf Groovy Markup Template ...
随机推荐
- Wireshark-TCP协议分析(包结构以及连接的建立和释放)
原文:http://blog.csdn.net/ahafg/article/details/51039584 TCP:传输控制协议 TCP是一种面向连接的.可靠的.基于字节流的传输层通信协议. 面向 ...
- Tablespace for table '`pomelo`.`bag`' exists. Please DISCARD the tablespace before IMPORT.
//遇到的问题是,删除数据库之后,重新创建数据库,在创建数据库表的时候,明明没有该表,却提示存在这个表.这是数据库缓存造成的 //解决方法 FLUSH TABLES; /* 安装MySql数据库(略) ...
- 基于zookeeper、连接池、Failover/LoadBalance等改造Thrift 服务化
对于Thrift服务化的改造,主要是客户端,可以从如下几个方面进行: 1.服务端的服务注册,客户端自动发现,无需手工修改配置,这里我们使用zookeeper,但由于zookeeper本身提供的客户端使 ...
- Android性能测试框架Emmagee
目前移动设备,尤其是Android设备,相对于过去的J2me时代,硬件有大幅度的提高,并且更新地非常快,可以安装和运行更多的应用. 但是目前安卓市场对于应用的要求并不如苹果商店那么严格,Android ...
- Spring HttpIvoker实现Java的远程调用
Spring HttpInvoker一种JAVA远程方法调用框架实现,使用的是HTTP协议,允许穿透防火墙,使用JAVA系列化方式,但仅限于Spring应用之间使用,即调用者与被调用者都必须是使用Sp ...
- (转)java泛型
转自:http://blog.csdn.net/lonelyroamer/article/details/7868820 java泛型(二).泛型的内部原理:类型擦除以及类型擦除带来的问题 参考:j ...
- H&M
H&M于1947年由Erling Persson在瑞典创立.如今,H&M在全世界1500 多个专卖店销售服装.配饰与化妆品.位于瑞典市Stora Gatan大街的老H&M店是世 ...
- iOS 功能代码 上传到 远程 码云私有库
推送代码到远程私有库 创建私有库(注意:仓库名称LYDKit必须和本地仓库对应的名称一样) 复制远程仓库的地址,打开终端,cd到对应的本地库路径下面 >>> cd /Users/cx ...
- UE打包32位程序遇到Win32 is not a supported platform for MindWaveEditor. Valid platforms are Win64.
1>------ 已启动全部重新生成: 项目: MindWave, 配置: Development_Editor Win32 ------1> Win32 is not a support ...
- webstorm配置内存参数,解决卡顿
找到WebStorm.exe.vmoptions这个文件,路径如下webstorm安装主目录>bin>WebStorm.exe.vmoptions更改为第二行:-Xms526m第三行:-X ...