On this page本页内容
$setField
New in version 5.0.在版本5.0中新增。
Adds, updates, or removes a specified field in a document.添加、更新或删除文档中的指定字段。
You can use 您可以使用$setField to add, update, or remove fields with names that contain periods (.) or start with dollar signs ($).$setField添加、更新或删除名称中包含句点(.)或以美元符号($)开头的字段。
$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 |
input | Object | field that you want to add or update. inputmissing, null, or undefined.missing、null或undefined。
|
value | Expression |
|
input evaluates to missing, undefined, or null, $setField returns null and does not update input.input的计算结果为missing、undefined或null,$setField将返回null,并且不会更新input。input evaluates to anything other than an object, missing, undefined, or null, $setField returns an error.input的计算结果不是对象、missing、undefined或为null,$setField将返回错误。field resolves to anything other than a string constant, $setField returns an error.field解析为字符串常量以外的任何值,$setField将返回错误。field doesn't exist in input, $setField adds it.input中不存在field,$setField会添加它。$setField$setField evaluates a field value 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": } } }。$unsetField is an alias for 是$setField with an input value of $$REMOVE. input值为$$REMOVE的$setField的别名。The following expressions are equivalent:以下表达式等效:
{
$setField: {
field: <field name>,
input: “$$ROOT”,
value: "$$REMOVE"
}
}
{
$unsetField: {
field: <field name>,
input: “$$ROOT”
}
}
.).)的字段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 }
]
$)$)开头的字段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". $replaceWith管道阶段和$setField和$literal运算符向每个文档添加一个新字段"$price"。The value of "$price" will equal the value of "price" in each document. "$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 }
]
.).)的字段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 }
]
$)$)开头的字段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 }
]
.).)的字段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"
} } }
] )
$)$)开头的字段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"
} } }
] )