Specify Which Fields to Return指定要返回的字段¶
Overview概述¶
Use a projection to control which fields appear in the documents returned by read operations. 使用投影控制哪些字段出现在读取操作返回的文档中。Many requests only require certain fields, so projections can help you limit unnecessary network bandwidth usage. 许多请求只需要某些字段,因此预测可以帮助您限制不必要的网络带宽使用。Projections work in two ways:预测工作有两种方式:
Explicitly include fields with a value of显式包含值为1
.1
的字段。This has the side-effect of implicitly excluding all unspecified fields.这样做的副作用是隐式排除所有未指定的字段。Implicitly exclude fields with a value of隐式排除值为0
.0
的字段。This has the side-effect of implicitly including all unspecified fields.这样做的副作用是隐式包含所有未指定的字段。
These two methods of projection are mutually exclusive: if you explicitly include fields, you cannot explicitly exclude fields, and vice versa.这两种投影方法是互斥的:如果显式包含字段,则不能显式排除字段,反之亦然。
Sample Documents样本文档¶
Consider the following collection containing documents that describe varieties of fruit:考虑下面的包含描述水果品种的文件:
[
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 },
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1 },
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 },
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 },
]
Your query operation may return a reference to a cursor that contains matching documents. 查询操作可能返回对包含匹配文档的游标的引用。To learn how to examine data stored in the cursor, see the Cursor Fundamentals page.要了解如何检查游标中存储的数据,请参阅游标基础知识页面。
Single Field单个字段¶
In the following query, pass the projection to only return the 在以下查询中,传递投影以仅返回每个文档的name
field of each document:name
字段:
// return only* the name field
const projection = { name: 1 };
const cursor = collection.find().project(projection);
await cursor.forEach(console.dir);
The projection document specifies a value of 投影文档为1
for name
to indicate that the read operation result should include the name
field of each returned document. name
指定值1
,以指示读取操作结果应包括每个返回文档的name
字段。As a result, this projection implicitly excludes the 因此,该投影隐式排除了qty
and rating
fields. qty
和rating
字段。Passing this projection to 将此投影传递给带有空查询文档且没有排序文档的find()
with an empty query document and no sort document yields the following results:find()
,将产生以下结果:
{ "_id": 1, "name": "apples" }
{ "_id": 2, "name": "bananas" }
{ "_id": 3, "name": "oranges" }
{ "_id": 4, "name": "avocados" }
Despite the fact that this projection only explicitly included the 尽管这个投影只显式包含name
field, the query returned the _id
field as well!name
字段,但查询也返回了_id
字段!
This happens because the 之所以会出现这种情况,是因为_id
field is a special case: it is always included in every query unless explicitly specified otherwise. _id
字段是一种特殊情况:除非另有明确规定,否则它总是包含在每个查询中。That's because 这是因为_id
is a unique identifier for each document, a property that can be very useful when constructing queries. _id
是每个文档的唯一标识符,这个属性在构造查询时非常有用。The movies
collection is a good example of why this property is useful: because remakes and even separate works can sometimes reuse movie titles, you need a unique _id
value to refer to any specific movie. movies
集合是一个很好的例子,说明了此属性的用途:因为重拍甚至单独的作品有时可以重用电影标题,所以您需要一个唯一的_id
值来引用任何特定的电影。_id
is the only exception to the mutually exclusive include-exclude behavior in projections: you can explicitly exclude _id
even when explicitly including other fields if you do not want _id
to be present in returned documents._id
是投影中互斥的include-exclude行为的唯一例外:如果不希望返回的文档中存在_id
,则即使在显式包含其他字段时,也可以显式排除_id
。
// return only the name field
const projection = { _id: 0, name: 1 };
const cursor = collection.find().project(projection);
await cursor.forEach(console.dir);
The projection document specifies a value of 投影文档为1
for name
to indicate that the read operation result should include the name
field of each returned document. name
指定值1
,以指示读取操作结果应包括每个返回文档的name字段。As a result, this projection implicitly excludes the 因此,该投影隐式排除了qty
and rating
fields. qty
和rating
字段。Passing this projection to 将此投影传递给带有空查询文档且没有排序文档的find()
with an empty query document and no sort document yields the following results:find()
,将产生以下结果:
{ "name": "apples" }
{ "name": "bananas" }
{ "name": "oranges" }
{ "name": "avocados" }
Multiple Fields多字段¶
You can also specify multiple fields to include in your projection. 还可以指定要包含在投影中的多个字段。Note: the order in which you specify the fields in the projection does not alter the order in which they are returned.注意:在投影中指定字段的顺序不会改变它们的返回顺序。
const projection = { _id: 0, rating: 1, name: 1 };
const cursor = collection.find().project(projection);
await cursor.forEach(console.dir);
This example that identifies two fields to include in the projection yields the following results:此示例确定了要包含在投影中的两个字段,并得出以下结果:
{ "name": "apples", "rating": 3 }
{ "name": "bananas", "rating": 1 }
{ "name": "oranges", "rating": 2 }
{ "name": "avocados", "rating": 5 }
For additional projection examples, see the MongoDB Manual page on Project Fields to Return from Query.有关其他投影示例,请参阅MongoDB手册中有关从查询返回的项目字段的页面。