Specify Which Fields to Return指定要返回的字段
On this page本页内容
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示例文档
To follow along with the examples in this guide, use the following code snippet to insert documents that describe fruits into the 要遵循本指南中的示例,请使用以下代码片段将描述水果的文档插入myDB.fruits
collection:myDB.fruits
集合:
const myDB = client.db("myDB");
const myColl = myDB.collection("fruits");
await myColl.insertMany([
{ "_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 = myColl.find().project(projection);
for await (const doc of cursor) {
console.dir(doc);
}
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集合就是一个很好的例子,说明了为什么这个属性很有用:因为翻拍甚至单独的作品有时可以重用电影标题,所以需要一个唯一的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. _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 = myColl.find().project(projection);
for await (const doc of cursor) {
console.dir(doc);
}
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 = myColl.find().project(projection);
for await (const doc of cursor) {
console.dir(doc);
}
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手册中关于从查询返回的项目字段的页面。