On this page本页内容
$project¶Passes along the documents with the requested fields to the next stage in the pipeline. 将带有请求字段的文档传递到管道中的下一个阶段。The specified fields can be existing fields from the input documents or newly computed fields.指定的字段可以是输入文档中的现有字段,也可以是新计算的字段。
The $project stage has the following prototype form:$project阶段具有以下原型形式:
The $project takes a document that can specify the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting of the values of existing fields. $project获取一个文档,该文档可以指定包含字段、抑制_id字段、添加新字段以及重置现有字段的值。Alternatively, you may specify the exclusion of fields.或者,您可以指定字段的排除。
The $project specifications have the following forms:$project规范有以下形式:
<field>: <1 or true> |
|
_id: <0 or false> |
|
<field>: <expression> |
|
<field>:<0 or false> |
|
_id field is, by default, included in the output documents. _id字段包含在输出文档中。$project.$project中明确指定包含项。$project ignores that field inclusion and does not add the field to the document.$project将忽略该字段包含,并且不会将该字段添加到文档中。_id Field_id字段¶By default, the 默认情况下,_id field is included in the output documents. _id字段包含在输出文档中。To exclude the 要从输出文档中排除_id field from the output documents, you must explicitly specify the suppression of the _id field in $project._id字段,必须在$project中明确指定对_id字段的抑制。
New in version 3.4.版本3.4中的新功能。
If you specify the exclusion of a field or fields, all other fields are returned in the output documents.如果指定排除一个或多个字段,则输出文档中将返回所有其他字段。
If you specify the exclusion of a field other than 如果指定排除_id, you cannot employ any other $project specification forms: i.e. if you exclude fields, you cannot also specify the inclusion of fields, reset the value of existing fields, or add new fields. _id以外的字段,则不能使用任何其他$project规范表单:即,如果排除字段,则也不能指定包含字段、重置现有字段的值或添加新字段。This restriction does not apply to conditional exclusion of a field using the 此限制不适用于使用REMOVE variable.REMOVE变量的字段的条件排除。
See also the 另请参见$unset stage to exclude fields.$unset阶段以排除字段。
New in version 3.6.版本3.6中的新功能。
Starting in MongoDB 3.6, you can use the variable 从MongoDB 3.6开始,可以在聚合表达式中使用变量REMOVE in aggregation expressions to conditionally suppress a field. REMOVE有条件地抑制字段。For an example, see Conditionally Exclude Fields.有关示例,请参阅条件排除字段。
Note
MongoDB also provides MongoDB还提供$addFields to add new fields to the documents.$addFields来向文档添加新字段。
To add a new field or to reset the value of an existing field, specify the field name and set its value to some expression. 要添加新字段或重置现有字段的值,请指定字段名称并将其值设置为某个表达式。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
To set a field value directly to a numeric or boolean literal, as opposed to setting the field to an expression that resolves to a literal, use the 要将字段值直接设置为数值或布尔文字,而不是将字段设置为解析为文字的表达式,请使用$literal operator. $literal运算符。Otherwise, 否则,$project treats the numeric or boolean literal as a flag for including or excluding the field.$project将数字或布尔文字视为包含或排除字段的标志。
By specifying a new field and setting its value to the field path of an existing field, you can effectively rename a field.通过指定新字段并将其值设置为现有字段的字段路径,可以有效地重命名字段。
Starting in MongoDB 3.2, 从MongoDB 3.2开始,$project stage supports using the square brackets [] to directly create new array fields. $project阶段支持使用方括号[]直接创建新的数组字段。If array specification includes fields that are non-existent in a document, the operation substitutes 如果数组规范包含文档中不存在的字段,则该操作会将null as the value for that field. null替换为该字段的值。For an example, see Project New Array Fields.有关示例,请参阅投影新数组字段。
When projecting or adding/resetting a field within an embedded document, you can either use dot notation, as in在嵌入文档中投影或添加/重置字段时,可以使用点表示法,如中所示
Or you can nest the fields:也可以嵌套字段:
When nesting the fields, you cannot use dot notation inside the embedded document to specify the field, e.g. 嵌套字段时,不能在嵌入文档中使用点符号指定字段,例如,contact: {
"address.country": <1 or 0 or expression> } is invalid.contact: {
"address.country": <1 or 0 or expression> }无效。
You cannot specify both an embedded document and a field within that embedded document in the same projection.不能在同一投影中同时指定嵌入文档和该嵌入文档中的字段。
The following 以下$project stage fails with a Path collision error because it attempts to project both the embedded contact document and the contact.address.country field:$project阶段失败,出现路径冲突错误,因为它试图投影嵌入的contact文档和contact.address.country字段:
The error occurs regardless of the order in which the parent document and embedded field are specified. 无论父文档和嵌入字段的指定顺序如何,都会发生此错误。The following 以下$project fails with the same error:$project失败,并出现相同错误:
Consider a 考虑一个books collection with the following document:books集合,有以下文档:
The following 以下$project stage includes only the _id, title, and the author fields in its output documents:$project阶段在其输出文档中仅包括_id、title和author字段:
The operation results in the following document:操作结果如下所示:
_id Field in the Output Documents_id字段¶The 默认情况下,始终包括_id field is always included by default. _id字段。To exclude the 要从_id field from the output documents of the $project stage, specify the exclusion of the _id field by setting it to 0 in the projection document.$project阶段的输出文档中排除_id字段,请通过在投影文档中将_id字段设置为0来指定排除_id字段。
Consider a 考虑一个books collection with the following document:books集合,有以下文档:
The following 以下$project stage excludes the _id field but includes the title, and the author fields in its output documents:$project阶段不包括_id字段,但在其输出文档中包括title和author字段:
The operation results in the following document:操作结果如下所示:
New in version 3.4.版本3.4中的新功能。
Consider a 考虑一个books collection with the following document:books集合,有以下文档:
The following 以下$project stage excludes the lastModified field from the output:$project阶段将lastModified字段从输出中排除:
See also the 另请参见$unset stage to exclude fields.$unset阶段以排除字段。
New in version 3.4.版本3.4中的新功能。
Consider a 考虑一个books collection with the following document:books集合,有以下文档:
The following 以下$project stage excludes the author.first and lastModified fields from the output:$project阶段从输出中排除author.first和lastModified字段:
Alternatively, you can nest the exclusion specification in a document:或者,您可以在文档中嵌套排除规范:
Both specifications result in the same output:两种规格产生相同的输出:
See also the 另请参见$unset stage to exclude fields.$unset阶段以排除字段。
New in version 3.6.版本3.6中的新功能。
Starting in MongoDB 3.6, you can use the variable 从MongoDB 3.6开始,可以在聚合表达式中使用变量REMOVE in aggregation expressions to conditionally suppress a field.REMOVE有条件地抑制字段。
Consider a 考虑一个books collection with the following document:books集合,有以下文档:
The following 以下$project stage uses the REMOVE variable to excludes the author.middle field only if it equals "":$project阶段仅在其等于""时使用REMOVE变量排除author.middle字段:
The aggregation operation results in the following output:聚合操作将产生以下输出:
Consider a 使用以下文档考虑bookmarks collection with the following documents:bookmarks集合:
To include only the 要在title field in the embedded document in the stop field, you can use the dot notation:stop字段中仅包含嵌入文档中的title字段,可以使用点表示法:
Or, you can nest the inclusion specification in a document:或者,您可以在文档中嵌套包含规范:
Both specifications result in the following documents:这两个规范产生了以下文件:
Consider a 考虑一个books collection with the following document:books集合,有以下文档:
The following 以下$project stage adds the new fields isbn, lastName, and copiesSold:$project阶段添加了新字段isbn、lastName和copiesSold:
The operation results in the following document:操作结果如下所示:
For example, if a collection includes the following document:例如,如果集合包含以下文档:
The following operation projects the fields 以下操作将字段x and y as elements in a new field myArray:x和y投影为新字段myArray中的元素:
The operation returns the following document:该操作将返回以下文档:
If array specification includes fields that are non-existent in a document, the operation substitutes 如果数组规范包含文档中不存在的字段,则该操作会将null as the value for that field.null替换为该字段的值。
For example, given the same document as above, the following operation projects the fields 例如,给定与上述相同的文档,以下操作将字段x, y, and a non-existing field $someField as elements in a new field myArray:x、y和不存在的字段$someField投影为新字段myArray中的元素:
The operation returns the following document:该操作将返回以下文档: