Database Manual / Reference / Query Language / Aggregation Stages

$set (aggregation stage)(聚合阶段)

Definition定义

Note

Disambiguation消歧

The following page refers to the aggregation stage $set. For the update operator $set, see $set.以下页面涉及聚合阶段$set。关于更新运算符$set,请参阅$set

$set

Adds new fields to documents. $set outputs documents that contain all existing fields from the input documents and newly added fields.向文档中添加新字段。$set输出包含输入文档中所有现有字段和新添加字段的文档。

The $set stage is an alias for $addFields.$set阶段是$addFields的别名。

Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.这两个阶段都相当于$project阶段,它明确指定输入文档中的所有现有字段并添加新字段。

Compatibility兼容性

You can use $set for deployments hosted in the following environments:您可以将$set用于在以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

$set has the following form:具有以下形式:

{ $set: { <newField>: <expression>, ... } }

Specify the name of each field to add and set its value to an aggregation expression or an empty object. 指定要添加的每个字段的名称,并将其值设置为聚合表达式或空对象。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Important

If the name of the new field is the same as an existing field name (including _id), $set overwrites the existing value of that field with the value of the specified expression.如果新字段的名称与现有字段名称(包括_id)相同,$set将用指定表达式的值覆盖该字段的现有值。

Behavior行为

  • $set appends new fields to existing documents. You can include one or more $set stages in an aggregation operation.将新字段附加到现有文档中。您可以在聚合操作中包含一个或多个$set阶段。
  • $set accepts the embedding of objects where you can set a value to an aggregation expression or to an empty object. For example, the following nested objects are accepted:接受对象的嵌入,您可以在其中为聚合表达式或空对象设置值。例如,接受以下嵌套对象:

    {$set: { a: { b: { } } } }

    To add a field or fields to embedded documents (including documents in arrays) use the dot notation. See example.要向嵌入式文档(包括数组中的文档)添加一个或多个字段,请使用点符号。参见示例

  • To add an element to an existing array field with $set, use with $concatArrays. See example.要使用$set向现有数组字段添加元素,请与$concatArrays一起使用。参见示例

Examples示例

MongoDB Shell

Using Two $set Stages使用两个$set阶段

Create a sample scores collection with the following:使用以下内容创建样本scores集合:

db.scores.insertMany( [
{ _id: 1, student: "Maya", homework: [ 10, 5, 10 ], quiz: [ 10, 8 ], extraCredit: 0 },
{ _id: 2, student: "Ryan", homework: [ 5, 6, 5 ], quiz: [ 8, 8 ], extraCredit: 8 }
] )

The following operation uses two $set stages to include three new fields in the output documents:以下操作使用两个$set阶段在输出文档中包含三个新字段:

db.scores.aggregate( [
{
$set: {
totalHomework: { $sum: "$homework" },
totalQuiz: { $sum: "$quiz" }
}
},
{
$set: {
totalScore: { $add: [ "$totalHomework", "$totalQuiz", "$extraCredit" ] } }
}
] )

The operation returns the following documents:该操作返回以下文档:

[
{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0,
totalHomework: 25,
totalQuiz: 18,
totalScore: 43
},
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8,
totalHomework: 16,
totalQuiz: 16,
totalScore: 40
}
]

Adding Fields to an Embedded Document向嵌入式文档添加字段

Use dot notation to add new fields to embedded documents.使用点符号向嵌入式文档添加新字段。

Create a sample collection vehicles with the following:使用以下工具创建样本集合vehicles(机动车):

db.vehicles.insertMany( [
{ _id: 1, type: "car", specs: { doors: 4, wheels: 4 } },
{ _id: 2, type: "motorcycle", specs: { doors: 0, wheels: 2 } },
{ _id: 3, type: "jet ski" }
] )

The following aggregation operation adds a new field fuel_type to the embedded document specs.以下聚合操作将一个新的字段fuel_type添加到嵌入式文档specs中。

db.vehicles.aggregate( [
{ $set: { "specs.fuel_type": "unleaded" } }
] )

The operation returns the following results:该操作返回以下结果:

[
{ _id: 1, type: "car", specs: { doors: 4, wheels: 4, fuel_type: "unleaded" } },
{ _id: 2, type: "motorcycle", specs: { doors: 0, wheels: 2, fuel_type: "unleaded" } },
{ _id: 3, type: "jet ski", specs: { fuel_type: "unleaded" } }
]

Overwriting an existing field覆盖现有字段

Specifying an existing field name in a $set operation causes the original field to be replaced.$set操作中指定现有字段名会导致替换原始字段。

Create a sample collection called animals with the following:使用以下内容创建一个名为animals的样本集合:

db.animals.insertOne( { _id: 1, dogs: 10, cats: 15 } )

The following $set operation overrides the cats field:以下$set操作将覆盖cats字段:

db.animals.aggregate( [
{ $set: { cats: 20 } }
] )

The operation returns the following document:该操作返回以下文档:

[ { _id: 1, dogs: 10, cats: 20 } ]

It is possible to replace one field with another. In the following example the item field substitutes for the _id field.可以用另一个字段替换一个字段。在以下示例中,item字段替换_id字段。

Create a sample collection called fruits contains the following documents:创建一个名为fruits的示例集合,其中包含以下文档:

db.fruits.insertMany( [
{ _id: 1, item: "tangerine", type: "citrus" },
{ _id: 2, item: "lemon", type: "citrus" },
{ _id: 3, item: "grapefruit", type: "citrus" }
] )

The following aggregration operation uses $set to replace the _id field of each document with the value of the item field, and replaces the item field with a string "fruit".以下聚合操作使用$set将每个文档的_id字段替换为item字段的值,并将item字段替换为字符串"fruit"

db.fruits.aggregate( [
{ $set: { _id: "$item", item: "fruit" } }
] )

The operation returns the following:该操作返回以下内容:

[
{ _id: "tangerine", item: "fruit", type: "citrus" },
{ _id: "lemon", item: "fruit", type: "citrus" },
{ _id: "grapefruit", item: "fruit", type: "citrus" }
]

Add Element to an Array向数组添加元素

Create a sample scores collection with the following:使用以下内容创建样本scores集合:

db.scores.insertMany( [
{ _id: 1, student: "Maya", homework: [ 10, 5, 10 ], quiz: [ 10, 8 ], extraCredit: 0 },
{ _id: 2, student: "Ryan", homework: [ 5, 6, 5 ], quiz: [ 8, 8 ], extraCredit: 8 }
] )

You can use $set with a $concatArrays expression to add an element to an existing array field. 您可以使用$set$concatArrays表达式将元素添加到现有数组字段中。For example, the following operation uses $set to replace the homework field with a new array whose elements are the current homework array concatenated with another array containing a new score [ 7 ].例如,以下操作使用$sethomework字段替换为一个新数组,该数组的元素是当前homework数组与另一个包含新分数的数组[7]连接在一起。

db.scores.aggregate( [
{ $match: { _id: 1 } },
{ $set: { homework: { $concatArrays: [ "$homework", [ 7 ] ] } } }
] )

The operation returns the following:该操作返回以下内容:

[ { _id: 1, student: "Maya", homework: [ 10, 5, 10, 7 ], quiz: [ 10, 8 ], extraCredit: 0 } ]

Creating a New Field with Existing Fields使用现有字段创建新字段

Create a sample scores collection with the following:使用以下内容创建样本scores集合:

db.scores.insertMany( [
{ _id: 1, student: "Maya", homework: [ 10, 5, 10 ], quiz: [ 10, 8 ], extraCredit: 0 },
{ _id: 2, student: "Ryan", homework: [ 5, 6, 5 ], quiz: [ 8, 8 ], extraCredit: 8 }
] )

The following aggregation operation adds a new field quizAverage to each document that contains the average of the quiz array.以下聚合操作将新字段quizAverage添加到包含quiz(测验)数组平均值的每个文档中。

db.scores.aggregate( [
{
$set: {
quizAverage: { $avg: "$quiz" }
}
}
] )

The operation returns the following documents:该操作返回以下文档:

[
{
_id: 1,
student: 'Maya',
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0,
quizAverage: 9
},
{
_id: 2,
student: 'Ryan',
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8,
quizAverage: 8
}
]
C#

The C# examples on this page use the sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see Get Started in the MongoDB .NET/C# Driver documentation.本页上的C#示例使用Atlas示例数据集中的sample_mflix数据库。要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅MongoDB NET/C#驱动程序文档中的入门

The following Movie class models the documents in the sample_mflix.movies collection:以下Movie类对sample_mflix.movies集合中的文档进行建模:

public class Movie
{
public ObjectId Id { get; set; }

public int Runtime { get; set; }

public string Title { get; set; }

public string Rated { get; set; }

public List<string> Genres { get; set; }

public string Plot { get; set; }

public ImdbData Imdb { get; set; }

public int Year { get; set; }

public int Index { get; set; }

public string[] Comments { get; set; }

[BsonElement("lastupdated")]
public DateTime LastUpdated { get; set; }
}

Note

ConventionPack for Pascal CasePascal大小写的约定包

The C# classes on this page use Pascal case for their property names, but the field names in the MongoDB collection use camel case. To account for this difference, you can use the following code to register a ConventionPack when your application starts:此页面上的C#类使用Pascal大小写作为其属性名,但MongoDB集合中的字段名使用驼峰大小写。为了解释这种差异,您可以在应用程序启动时使用以下代码注册ConventionPack

var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);

To use the MongoDB .NET/C# driver to add a $set stage to an aggregation pipeline, call the Set() method on a PipelineDefinition object.要使用MongoDB .NET/C#驱动程序向聚合管道添加$set stage,请在PipelineDefinition对象上调用Set()方法。

The following example creates a pipeline stage that sets the value of the LastUpdated field in each Movie document to the value of DateTime.Now:以下示例创建了一个管道阶段,该阶段将每个Movie文档中LastUpdated字段的值设置为DateTime.Now的值:

var pipeline = new EmptyPipelineDefinition<Movie>()
.Set(Builders<Movie>.SetFields.Set(m => m.LastUpdated, DateTime.Now));
Node.js

The Node.js examples on this page use the sample_mflix database from the Atlas sample datasets. 本页上的Node.js示例使用Atlas示例数据集中的sample_mflix数据库。To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see Get Started in the MongoDB Node.js driver documentation.要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅MongoDB Node.js驱动程序文档中的入门

To use the MongoDB Node.js driver to add a $set stage to an aggregation pipeline, use the $set operator in a pipeline object.要使用MongoDB Node.js驱动程序向聚合管道添加$set stage,请在管道对象中使用$set运算符。

The following example creates a pipeline stage that sets the value of the lastupdated field in each movie document to the value of the Date object. The example then runs the aggregation pipeline:以下示例创建了一个管道阶段,该阶段将每个电影文档中lastupdated字段的值设置为Date对象的值。然后,该示例运行聚合管道:

const pipeline = [{ $set: { lastupdated: new Date() } }];

const cursor = collection.aggregate(pipeline);
return cursor;

Learn More了解更多

To learn more about related pipeline stages, see the $addFields guide.要了解有关相关管道阶段的更多信息,请参阅$addFields指南。