文档菜单
文档首页
/
MongoDB 手册
/ / /

设置字段(聚合)

本页内容

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

版本5.0.

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

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

提示

使用$getField 用于检索包含美元符号 ($) 或点号 (.) 的字段的值,这些字段是通过 $setField 添加或更新的。

$setField 语法如下

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

必须提供以下字段

字段
类型
描述
field
String
要在 input 对象中添加、更新或删除的字段。 field 可以是任何有效的 表达式,该表达式解析为字符串常量。
input
Object
包含要添加或更新的 field 的文档。 input 必须解析为对象、missingnullundefined
value
Expression

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

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

  • 如果 input 评估为 missingundefinednull,则 $setField 返回 null 并不会更新 input

  • 如果 input 评估为除了对象以外的任何值,missingundefinednull,则 $setField 返回一个错误。

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

  • 如果 field 不存在于 input 中,则 $setField 会添加它。

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

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

    {
    $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"
} } }
] )

提示

另请参阅

返回

集合等价