$setField (aggregation)
On this page本页内容
Definition定义
$setFieldNew in version 5.0.5.0版新增。Adds, updates, or removes a specified field in a document.添加、更新或删除文档中的指定字段。You can use您可以使用$setFieldto add, update, or remove fields with names that contain periods (.) or start with dollar signs ($).$setField添加、更新或删除名称包含句点(.)或以美元符号($)开头的字段。TipUse使用$getFieldto retrieve the values of fields that contain dollar signs ($) or periods (.) that you add or update with$setField.$getField检索包含您使用$setField添加或更新的美元符号($)或句点(.)的字段的值。
Syntax语法
$setField has the following syntax:具有以下语法:
{
$setField: {
field: <String>,
input: <Object>,
value: <Expression>
}
}
You must provide the following fields:您必须提供以下字段:
field | String | input object that you want to add, update, or remove. input对象中的字段。field can be any valid expression that resolves to a string constant.field可以是解析为字符串常量的任何有效表达式。 |
input | Object | field that you want to add or update. field的文档。input must resolve to an object, missing, null, or undefined.input必须解析为对象、missing、null或undefined。 |
value | Expression | field. field的值。value$$REMOVE to remove field from the input document. $$REMOVE可从input文档中删除field。 |
Behavior行为
If如果inputevaluates tomissing,undefined, ornull,$setFieldreturnsnulland does not updateinput.input的计算结果为missing、undefined或null,则$setField将返回null并且不更新输入。If如果inputevaluates to anything other than an object,missing,undefined, ornull,$setFieldreturns an error.input的求值结果不是对象、missing、undefined或为null,那么$setField将返回一个错误。If若fieldresolves to anything other than a string constant,$setFieldreturns an error.field解析为字符串常量以外的任何值,则$setField将返回一个错误。If若fielddoesn't exist ininput,$setFieldadds it.input中不存在field,则$setField将添加该字段。$setFielddoesn't implicitly traverse objects or arrays.不隐式遍历对象或数组。For example,例如,$setFieldevaluates afieldvalue of"a.b.c"as a top-level field"a.b.c"instead of as a nested field,{ "a": { "b": { "c": } } }.$setField将field值"a.b.c"计算为顶级字段"a.b.c",而不是嵌套字段{ "a": { "b": { "c": } } }。$unsetFieldis an alias for是输入值为$setFieldwith an input value of$$REMOVE.$$REMOVE的$setField的别名。The following expressions are equivalent:以下表达式等效:{
$setField: {
field: <field name>,
input: “$$ROOT”,
value: "$$REMOVE"
}
}
{
$unsetField: {
field: <field name>,
input: “$$ROOT”
}
}
See also: 另请参阅:
Examples实例
Add Fields that Contain Periods (.)添加包含句点(.)的字段
.)Consider an 考虑一个包含以下文档的inventory collection with the following documents:inventory集合:
db.inventory.insertMany( [
{ "_id" : 1, "item" : "sweatshirt", price: 45.99, qty: 300 }
{ "_id" : 2, "item" : "winter coat", price: 499.99, qty: 200 }
{ "_id" : 3, "item" : "sun dress", price: 199.99, qty: 250 }
{ "_id" : 4, "item" : "leather boots", price: 249.99, qty: 300 }
{ "_id" : 5, "item" : "bow tie", price: 9.99, qty: 180 }
] )
The following operation uses the 以下操作使用$replaceWith pipeline stage and the $setField operator to add a new field to each document, "price.usd". $replaceWith管道阶段和$setField运算符为每个文档添加一个新字段"price.usd"。The value of "price.usd" will equal the value of "price" in each document. "price.usd"的值将等于每个文档中"price"的值。Finally, the operation uses the 最后,该操作使用$unset pipeline stage to remove the "price" field.$unset管道阶段来删除"price"字段。
db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: "$price"
} } },
{ $unset: "price" }
] )
The operation returns the following results:该操作返回以下结果:
[
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
]
Add Fields that Start with a Dollar Sign ($)添加以美元符号($)开头的字段
$)Consider an 考虑一个包含以下文档的inventory collection with the following documents:inventory集合:
db.inventory.insertMany( [
{ "_id" : 1, "item" : "sweatshirt", price: 45.99, qty: 300 }
{ "_id" : 2, "item" : "winter coat", price: 499.99, qty: 200 }
{ "_id" : 3, "item" : "sun dress", price: 199.99, qty: 250 }
{ "_id" : 4, "item" : "leather boots", price: 249.99, qty: 300 }
{ "_id" : 5, "item" : "bow tie", price: 9.99, qty: 180 }
] )
The following operation uses the 以下操作使用$replaceWith pipeline stage and the $setField and $literal operators to add a new field to each document, "$price". The value of "$price" will equal the value of "price" in each document. $replaceWith管道阶段以及$setField和$literal运算符为每个文档添加一个新字段"$price"。"$price"的值将等于每个文档中的"$price"值。Finally, the operation uses the 最后,该操作使用$unset pipeline stage to remove the "price" field.$unset管道阶段来删除"price"字段。
db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: "$price"
} } },
{ $unset: "price" }
] )
The operation returns the following results:该操作返回以下结果:
[
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
]
Update Fields that Contain Periods (.)更新包含期间的字段(.)
.)Consider an 考虑一个包含以下文档的inventory collection with the following documents:inventory集合:
db.inventory.insertMany( [
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
] )
The following operation uses the 以下操作使用$match pipeline stage to find a specific document and the $replaceWith pipeline stage and the $setField operator to update the "price.usd" field in the matching document:$match管道阶段查找特定文档,使用$replaceWith管道阶段和$setField运算符更新匹配文档中的"price.usd"字段:
db.inventory.aggregate( [
{ $match: { _id: 1 } },
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: 49.99
} } }
] )
The operation returns the following results:该操作返回以下结果:
[
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 49.99 }
]
Update Fields that Start with a Dollar Sign ($)更新以美元符号($)开头的字段
$)Consider an 考虑一个包含以下文档的inventory collection with the following documents:inventory集合:
db.inventory.insertMany([
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
] )
The following operation uses the 以下操作使用$match pipeline stage to find a specific document and the $replaceWith pipeline stage and the $setField and $literal operators to update the "$price" field in the matching document:$match管道阶段查找特定文档,使用$replaceWith管道阶段以及$setField和$literal运算符更新匹配文档中的"$price"字段:
db.inventory.aggregate( [
{ $match: { _id: 1 } },
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: 49.99
} } }
] )
The operation returns the following results:该操作返回以下结果:
[
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 49.99 }
]
Remove Fields that Contain Periods (.)删除包含句点(.)的字段
.)Consider an 考虑一个包含以下文档的inventory collection with the following documents:inventory集合:
db.inventory.insertMany([
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
] )
The following operation uses the 以下操作使用$replaceWith pipeline stage and the $setField operator and $$REMOVE to remove the "price.usd" field from each document:$replaceWith管道阶段、$setField运算符和$$REMOVE 从每个文档中删除"price.usd"字段:
db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: "$$REMOVE"
} } }
] )
The operation returns the following results:该操作返回以下结果:
[
{ _id: 1, item: 'sweatshirt', qty: 300 },
{ _id: 2, item: 'winter coat', qty: 200 },
{ _id: 3, item: 'sun dress', qty: 250 },
{ _id: 4, item: 'leather boots', qty: 300 },
{ _id: 5, item: 'bow tie', qty: 180 }
]
A similar query written using the 使用$unsetField alias returns the same results:$unsetField别名编写的类似查询返回相同的结果:
db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: "price.usd",
input: "$$ROOT"
} } }
] )
Remove Fields that Start with a Dollar Sign ($)删除以美元符号($)开头的字段
$)Consider an 考虑一个包含以下文档的inventory collection with the following documents:inventory集合:
db.inventory.insertMany( [
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
} )
The following operation uses the 以下操作使用$replaceWith pipeline stage, the $setField and $literal operators, and $$REMOVE to remove the "$price" field from each document:$replaceWith管道阶段、$setField和$literal运算符以及$$REMOVE从每个文档中删除"$price"字段:
db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: "$$REMOVE"
} } }
] )
The operation returns the following results:该操作返回以下结果:
[
{ _id: 1, item: 'sweatshirt', qty: 300 },
{ _id: 2, item: 'winter coat', qty: 200 },
{ _id: 3, item: 'sun dress', qty: 250 },
{ _id: 4, item: 'leather boots', qty: 300 },
{ _id: 5, item: 'bow tie', qty: 180 }
]
A similar query written using the 使用$unsetField alias returns the same results:$unsetField别名编写的类似查询返回相同的结果:
db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: { $literal: "$price" },
input: "$$ROOT"
} } }
] )