12.Project Fields to Return from Query-官方文档摘录

本文介绍如何使用MongoDB进行高效的数据查询与字段选择,包括基本查询、字段选择、内嵌文档及数组元素的选择等操作,并提供具体的SQL对应示例。

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:

Copy
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":

Copy
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 itemstatus and, by default, the _id fields return in the matching documents.

Copy
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:

Copy
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:

Copy
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.

Copy
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:

Copy
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.

Copy
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.

Copy
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": }projection will not project the array with the first element.

SEE ALSO

Query Documents

 

转载于:https://www.cnblogs.com/olinux/p/7261507.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值