定义
$setField版本 5.0 中的新增功能。
添加、更新或删除文档中的指定字段。
You can use
$setFieldto add, update, or remove fields with names that contain periods (.) or start with dollar signs ($).请勿依赖与保留的内部元数据字段名称冲突的根级前缀为美元的字段名称,因为在某些查询处理上下文中,这些字段可能会被删除或无法访问。有关包含美元符 (
$)或句号 (.)的字段名称的限制和指导,请参阅带有句号和美元符的字段名称。提示
Use
$getFieldto retrieve the values of fields that contain dollar signs ($) or periods (.) that you add or update with$setField.
语法
$setField 通过以下语法实现:
{ $setField: { field: <String>, input: <Object>, value: <Expression> } }
您必须提供以下字段:
行为
If
inputevaluates tomissing,undefined, ornull,$setFieldreturnsnulland does not updateinput.If
inputevaluates to anything other than an object,missing,undefined, ornull,$setFieldreturns an error.If
fieldresolves to anything other than a string constant,$setFieldreturns an error.If
fielddoesn't exist ininput,$setFieldadds it.$unsetFieldis an alias for$setFieldwith an input value of$$REMOVE. The following expressions are equivalent:{ $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 } ] )
The following operation uses the $replaceWith pipeline stage and the $setField operator to add a new field to each document, "price.usd". The value of "price.usd" equals the value of "price" in each document. Finally, the operation uses the $unset pipeline stage to remove the "price" field.
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 } ] )
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" equals the value of "price" in each document. Finally, the operation uses the $unset pipeline stage to remove the "price" field.
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 } ] )
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:
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 } ] )
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:
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 } ] )
The following operation uses the $replaceWith pipeline stage and the $setField operator and $$REMOVE to remove the "price.usd" field from each document:
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 } ] )
The following operation uses the $replaceWith pipeline stage, the $setField and $literal operators, and $$REMOVE to remove the "$price" field from each document:
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" } } } ] )