Project Fields to Return from Query要从查询返回的项目字段
On this page本页内容
Return All Fields in Matching Documents返回匹配文档中的所有字段Return the Specified Fields and the仅返回指定字段和_id
Field Only_id
字段Suppress抑制_id
Field_id
字段Return All But the Excluded Fields返回除已排除字段外的所有字段Return Specific Fields in Embedded Documents返回嵌入文档中的特定字段Suppress Specific Fields in Embedded Documents抑制嵌入文档中的特定字段Projection on Embedded Documents in an Array数组中嵌入文档的投影Project Specific Array Elements in the Returned Array返回数组中的项目特定数组元素Additional Considerations其他注意事项
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.
By default, queries in MongoDB return all fields in matching documents. 默认情况下,MongoDB中的查询返回匹配文档中的所有字段。To limit the amount of data that MongoDB sends to applications, you can include a projection document to specify or restrict fields to return.为了限制MongoDB发送给应用程序的数据量,可以包含一个投影文档来指定或限制要返回的字段。
This page provides examples of query operations with projection using the Collection.find() method in the MongoDB Node.js Driver.本页提供了使用MongoDB Node.js驱动程序中的Collection.find()
方法进行投影的查询操作示例。
The examples on this page use the 此页面上的示例使用inventory
collection. inventory
集合。Connect to a test database in your MongoDB instance then create the 连接到MongoDB实例中的测试数据库,然后创建inventory
collection:inventory
集合:
await db.collection('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 }
]
}
]);
Return All Fields in Matching Documents返回匹配文档中的所有字段
If you do not specify a projection document, the find() method yields all fields in the matching documents.如果没有指定投影文档,find()
方法将生成匹配文档中的所有字段。
The following example returns all fields from all documents in the 以下示例返回inventory
collection where the status
equals "A"
:inventory
集合中status
等于"A"
的所有文档中的所有字段:
const cursor = db.collection('inventory').find({
status: 'A'
});
The operation corresponds to the following SQL statement:该操作对应于以下SQL语句:
SELECT * from inventory WHERE status = "A"
Return the Specified Fields and the _id
Field Only仅返回指定字段和_id
字段
_id
Field OnlyA projection can explicitly include several fields by setting the 通过在投影文档中将<field>
to 1
in the projection document. <field>
设置为1
,投影可以显式地包括多个字段。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.item
、status
以及默认情况下返回的_id
字段。
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1 });
The operation corresponds to the following SQL statement:该操作对应于以下SQL语句:
SELECT _id, item, status from inventory WHERE status = "A"
Suppress _id
Field抑制_id
字段
_id
FieldYou can remove the 您可以通过在投影中将_id
field from the results by setting it to 0
in the projection, as in the following example:_id
字段设置为0
从结果中删除,如下例所示:
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, _id: 0 });
The operation corresponds to the following SQL statement:该操作对应于以下SQL语句:
SELECT item, status from inventory WHERE status = "A"
With the exception of the 除了_id
field, you cannot combine inclusion and exclusion statements in projection documents._id
字段外,您不能在投影文档中组合包含语句和排除语句。
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:status
和instock
字段之外的所有字段:
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ status: 0, instock: 0 });
With the exception of the 除了_id
field, you cannot combine inclusion and exclusion statements in projection documents._id
字段外,您不能在投影文档中组合包含语句和排除语句。
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.1
。
The following example returns:以下示例返回:
The_id
field (returned by default),_id
字段(默认返回),Theitem
field,item
字段,Thestatus
field,status
字段,Theuom
field in thesize
document.size
文档中的uom
字段。
The uom
field remains embedded in the size
document.uom
字段仍然嵌入在size
文档中。
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, 'size.uom': 1 });
Starting in MongoDB 4.4, you can also specify embedded fields using the nested form, e.g. 从MongoDB 4.4开始,您还可以使用嵌套形式指定嵌入字段,例如{ item: 1, status: 1, size: { uom: 1 } }
.{ 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
.0
。
The following example specifies a projection to exclude the 以下示例指定了一个投影,以排除uom
field inside the size
document. size
文档中的uom
字段。All other fields are returned in the matching documents:所有其他字段都会在匹配的文档中返回:
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 'size.uom': 0 });
Starting in MongoDB 4.4, you can also specify embedded fields using the nested form, e.g. 从MongoDB 4.4开始,您还可以使用嵌套形式指定嵌入字段,例如{ size: { uom: 0 } }
.{ 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_id
field (returned by default),_id
字段(默认返回),Theitem
field,item
字段,Thestatus
field,status
字段,Theqty
field in the documents embedded in theinstock
array.instock
数组中嵌入的文档中的qty
字段。
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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 for manipulating arrays: 对于包含数组的字段,MongoDB提供了以下投影运算符来操作数组:$elemMatch
, $slice
, and $
.$elemMatch
、$slice
和$
。
The following example uses the 以下示例使用$slice
projection operator to return the last element in the instock
array:$slice
投影运算符返回instock
数组中的最后一个元素:
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, instock: { $slice: -1 } });
$elemMatch
, $slice
, and $
are the only way to project specific elements to include in the returned array. $elemMatch
、$slice
和$
是投影要包含在返回数组中的特定元素的唯一方法。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.{ "instock.0": 1 }
投影不会投影具有第一个元素的数组。
Additional Considerations其他注意事项
Starting in MongoDB 4.4, MongoDB enforces additional restrictions with regards to projections. 从MongoDB 4.4开始,MongoDB对投影实施了额外的限制。See Projection Restrictions for details.有关详细信息,请参阅投影限制。