This page provides examples in:本页提供了以下示例:
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 本页提供了使用mongo shell中的db.collection.find()
method in the mongo
shell. db.collection.find()
方法进行投影的查询操作示例。The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations with projection using MongoDB Compass. 本页提供了使用MongoDB Compass进行投影的查询操作示例。The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。Populate the 使用以下文档填充inventory
collection with the following documents:inventory
集合:
This page provides examples of query operations with projection using the 本页提供了使用pymongo Python驱动程序中的pymongo.collection.Collection.find()
method in the PyMongo Python driver. pymongo.collection.Collection.find()
方法进行投影的查询操作示例。The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations with projection using the com.mongodb.client.MongoCollection.find method in the MongoDB Java Synchronous Driver.本页提供了使用mongodb Java同步驱动程序中的com.mongodb.client.MongoCollection.find
方法进行投影的查询操作示例。
Tip
The driver provides com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. 驱动程序提供com.mongodb.client.model.Filters
帮助程序方法,以方便创建筛选文档。The examples on this page use these methods to create the filter documents.本页上的示例使用这些方法创建筛选文档。
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
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
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations with projection using the MongoDB\Collection::find()
method in the MongoDB PHP Library. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations with projection using the motor.motor_asyncio.AsyncIOMotorCollection.find()
method in the Motor driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations with projection using the com.mongodb.reactivestreams.client.MongoCollection.find method in the MongoDB Java Reactive Streams Driver.本页提供了使用mongodb Java反应流驱动程序中的com.mongodb.reactivestreams.client.MongoCollection.findcom.mongodb.reactivestreams.client.MongoCollection.find方法进行投影的查询操作示例。
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations with projection using the MongoCollection.Find() method in the MongoDB C# Driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations with projection using the MongoDB::Collection::find() method in the MongoDB Perl Driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations with projection using the Mongo::Collection#find() method in the MongoDB Ruby Driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations with projection using the collection.find() method in the MongoDB Scala Driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations with projection using the Collection.Find function in the MongoDB Go Driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
You can run the operation in the web shell below:您可以在下面的web shell中运行该操作:
For instructions on inserting documents in MongoDB Compass, see Insert Documents.有关在MongoDB Compass中插入文档的说明,请参阅插入文档。
If you do not specify a projection document, the 如果未指定投影文档,db.collection.find()
method returns all fields in the matching documents.db.collection.find()
方法将返回匹配文档中的所有字段。
If you do not specify a projection document, Compass returns all fields in the matching documents.如果未指定投影文档,Compass将返回匹配文档中的所有字段。
If you do not specify a projection document, the find()
method returns all fields in the matching documents.
If you do not specify a projection, the com.mongodb.client.MongoCollection.find method returns all fields in the matching documents.如果未指定投影,则com.mongodb.client.MongoCollection.find
方法将返回匹配文档中的所有字段。
If you do not specify a projection document, the find() method yields all fields in the matching documents.如果未指定投影文档,则find()
方法将生成匹配文档中的所有字段。
If you do not specify a projection document, the find()
method returns all fields in the matching documents.
If you do not specify a projection, the com.mongodb.reactivestreams.client.MongoCollection.find method returns all fields in the matching documents.如果未指定投影,com.mongodb.reactivestreams.client.MongoCollection.find
方法将返回匹配文档中的所有字段。
If you do not specify a projection filter, the MongoCollection.Find() method returns all fields in the matching documents.
If you do not specify a projection document, the find() method returns all fields in the matching documents.
If you do not specify a projection document, the find() method returns all fields in the matching documents.
If you do not specify a projection, the 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 status
equals "A"
:inventory
集合中status
为"A"
的所有文档中的所有字段:
The operation corresponds to the following SQL statement:该操作对应于以下SQL语句:
_id
Field Only_id
字段¶A 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
字段。
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将com.mongodb.client.FindIterable.projection方法连缀到find
method. find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
The operation corresponds to the following SQL statement:该操作对应于以下SQL语句:
_id
Field_id
字段¶You can remove the 通过在投影中将_id
field from the results by setting it to 0
in the projection, as in the following example:_id
字段设置为0
,可以从结果中删除该字段,如下例所示:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
The operation corresponds to the following SQL statement:该操作对应于以下SQL语句:
Note
With the exception of the 除了_id
field, you cannot combine inclusion and exclusion statements in projection documents._id
字段之外,您不能在投影文档中组合包含和排除语句。
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
字段外的所有字段:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
Note
With the exception of the 除了_id
field, you cannot combine inclusion and exclusion statements in projection documents._id
字段之外,您不能在投影文档中组合包含和排除语句。
You can return specific fields in an embedded document. 可以在嵌入文档中返回特定字段。Use the dot notation to refer to the embedded field and set to 使用点表示法引用嵌入字段,并在投影文档中设置为1。1
in the projection document.
The following example returns:以下示例返回:
_id
field (returned by default),_id
字段(默认情况下返回),item
field,item
字段,status
field,status
字段,uom
field in the size
document.size
文档中的uom
字段。The uom
field remains embedded in the size
document.uom
字段仍嵌入在size
文档中。
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
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 } }
。
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:所有其他字段将在匹配文档中返回:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
Starting in MongoDB 4.4, you can also specify embedded fields using the nested form, e.g. 从MongoDB 4.4开始,还可以使用嵌套形式指定嵌入字段,例如{ size: { uom: 0 } }
.{ item: 1, status: 1, size: { uom: 1 } }
。
Use dot notation to project specific fields inside documents embedded in an array.使用点表示法在嵌入数组的文档中投影特定字段。
The following example specifies a projection to return:以下示例指定要返回的投影:
_id
field (returned by default),_id
字段(默认情况下返回),item
field,item
字段,status
field,status
字段qty
field in the documents embedded in the instock
array.instock
数组中嵌入的文档中的qty
字段。To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
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
数组中的最后一个元素:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$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 }
投影将不会投影具有第一个元素的数组。
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
数组中的最后一个元素:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
For fields that contain arrays, MongoDB provides the following projection operators for manipulating arrays: $elemMatch
, $slice
, and $
.
The following example uses the $slice
projection operator to return the last element in the instock
array:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$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 }
投影将不会投影具有第一个元素的数组。
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
数组中的最后一个元素:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$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. 例如,不能使用数组索引投影特定数组元素;例如,include("instock.0")
projection will not project the array with the first element.include("instock.0")
投影将不会投影具有第一个元素的数组。
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
数组中的最后一个元素:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$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 }
投影将不会投影具有第一个元素的数组。
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
数组中的最后一个元素:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$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 ]
投影不会投影具有第一个元素的数组。
For fields that contain arrays, MongoDB provides the following projection operators for manipulating arrays: $elemMatch
, $slice
, and $
.
The following example uses the 以下示例使用$slice
projection operator to return the last element in the instock
array:$slice
投影运算符返回instock
数组中的最后一个元素:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$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. 例如,不能使用数组索引投影特定的数组元素;例如,include("instock.0")
projection will not project the array with the first element.include("instock.0")
投影不会投影第一个元素的数组。
For fields that contain arrays, MongoDB provides the following projection operators for manipulating arrays: $elemMatch
, $slice
, and $
.
The following example uses the $slice
projection operator to return the last element in the instock
array:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$elemMatch
, $slice
, and $
are the only way to project specific elements to include in the returned array.
For example, the following operation will not project the array with the first element:
For fields that contain arrays, MongoDB provides the following projection operators for manipulating arrays: $elemMatch
, $slice
, and $
.
The following example uses the $slice
projection operator to return the last element in the instock
array:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$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.
For fields that contain arrays, MongoDB provides the following projection operators for manipulating arrays: $elemMatch
, $slice
, and $
.
The following example uses the $slice
projection operator to return the last element in the instock
array:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$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.
For fields that contain arrays, MongoDB provides the following projection operators for manipulating arrays: $elemMatch
, $slice
, and $
.
The following example uses the $slice
projection operator to return the last element in the instock
array:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$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. include("instock.0")
projection will not project the array with the first element.
For fields that contain arrays, MongoDB provides the following projection operators for manipulating arrays: $elemMatch
, $slice
, and $
.
The following example uses the $slice
projection operator to return the last element in the instock
array:
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the 要指定投影文档,请将find
method. com.mongodb.client.FindIterable.projection
方法连缀到find
方法后面。The example uses the com.mongodb.client.model.Projections class to create the projection documents.该示例使用com.mongodb.client.model.Projections
类创建投影文档。
$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. include("instock.0")
projection will not project the array with the first element.
Starting in MongoDB 4.4, MongoDB enforces additional restrictions with regards to projections. 从MongoDB 4.4开始,MongoDB对投影实施额外的限制。See 有关详细信息,请参阅投影限制。Projection Restrictions
for details.
See also参阅