Docs 菜单

Docs 主页开发应用程序MongoDB Manual

$setField(聚合)

在此页面上

  • 定义
  • 语法
  • 行为
  • 举例
$setField

版本 5.0 中的新增功能

添加、更新或删除文档中的指定字段。

您可以使用 $setField添加、更新或删除名称包含句点 ( . ) 或以美元符号 ( $ ) 开头的字段。

提示

使用$getField检索您使用$setField添加或更新的包含美元符号 ( $ ) 或句点 ( . ) 的字段的值。

$setField 通过以下语法实现:

{
$setField: {
field: <String>,
input: <Object>,
value: <Expression>
}
}

您必须提供以下字段:

字段
类型
说明
field
字符串
您要添加、更新或删除的 input 对象中的字段。field 可以是解析为字符串常量的任何有效表达式
input
对象
一份文档,包含您要添加或更新的 fieldinputmissing 必须解析为对象、null、或 undefined
value
表达式(expression)

要分配给 field 的值。value 可以是任何有效表达式

设为 $$REMOVE 可从 input 文档中删除 field

  • 如果input的计算结果为missingundefinednull ,则$setField返回null并且不更新input

  • 如果input的计算结果为对象、 missingundefinednull以外的任何值, $setField将返回错误。

  • 如果field解析为字符串常量以外的任何值, $setField将返回错误。

  • 如果field input$setField 则会进行添加。

  • $setField不会隐式遍历对象或数组。例如, $setField"a.b.c"field值计算为顶级字段"a.b.c" ,而不是嵌套字段{ "a": { "b": { "c": } } }

  • $unsetField是输入值为$$REMOVE$setField的别名。以下表达式是等效的:

    {
    $setField: {
    field: <field name>,
    input: “$$ROOT”,
    value: "$$REMOVE"
    }
    }
    {
    $unsetField: {
    field: <field name>,
    input: “$$ROOT”
    }
    }

提示

另请参阅:

考虑包含以下文档的 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 }
] )

以下操作使用$replaceWith管道阶段和$setField操作符为每个文档"price.usd"添加一个新字段。每个文档中"price.usd"的值将等于"price"的值。最后,该操作使用$unset管道阶段删除"price"字段。

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: "$price"
} } },
{ $unset: "price" }
] )

操作返回以下结果:

[
{ _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 }
]

考虑包含以下文档的 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 }
] )

以下操作使用$replaceWith管道阶段以及$setField$literal操作符向每个文档"$price"添加一个新字段。每个文档中"$price"的值将等于"price"的值。最后,该操作使用$unset管道阶段删除"price"字段。

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: "$price"
} } },
{ $unset: "price" }
] )

操作返回以下结果:

[
{ _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 }
]

考虑包含以下文档的 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 }
] )

以下操作使用$match管道阶段查找特定文档,并使用$replaceWith管道阶段和$setField操作符更新匹配文档中的"price.usd"字段:

db.inventory.aggregate( [
{ $match: { _id: 1 } },
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: 49.99
} } }
] )

操作返回以下结果:

[
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 49.99 }
]

考虑包含以下文档的 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 }
] )

以下操作使用$match管道阶段查找特定文档,并使用$replaceWith管道阶段以及$setField$literal操作符更新匹配文档中的"$price"字段:

db.inventory.aggregate( [
{ $match: { _id: 1 } },
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: 49.99
} } }
] )

操作返回以下结果:

[
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 49.99 }
]

考虑包含以下文档的 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 }
] )

以下操作使用$replaceWith管道阶段以及$setField操作符和$$REMOVE从每个文档中删除"price.usd"字段:

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: "$$REMOVE"
} } }
] )

操作返回以下结果:

[
{ _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 }
]

使用 $unsetField 别名写入的类似查询会返回相同的结果:

db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: "price.usd",
input: "$$ROOT"
} } }
] )

考虑包含以下文档的 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 }
} )

以下操作使用$replaceWith管道阶段、 $setField$literal操作符以及$$REMOVE从每个文档中删除"$price"字段:

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: "$$REMOVE"
} } }
] )

操作返回以下结果:

[
{ _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 }
]

使用 $unsetField 别名写入的类似查询会返回相同的结果:

db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: { $literal: "$price" },
input: "$$ROOT"
} } }
] )

提示

另请参阅:

← $setEquals(聚合)