This section summarizes how different insert and update operations handle dollar (本节总结了不同的插入和更新操作如何处理以美元($) prefixed field names. MongoDB discourages the use of dollar prefixed field names because some features aren't supported with these fields. $)为前缀的字段名。MongoDB不鼓励使用以美元为前缀的字段名,因为这些字段不支持某些功能。See General Restrictions for more information.有关更多信息,请参阅一般限制。
Insert Operations插入操作
Dollar (允许使用美元($) prefixed fields are permitted as top level and nested field names for inserts.$)前缀字段作为插入的顶级和嵌套字段名。
db.sales.insertOne( {
"$price": 50.00,
"quantity": 30
} )
Dollar (使用保留字插入时,允许使用美元($) prefixed fields are permitted on inserts using otherwise reserved words. $)前缀字段。Operator names like 像$inc can be used as field names as well as words like id, db, and ref.$inc这样的运算符名称既可以用作字段名称,也可以用作id、db和ref这样的单词。
db.books.insertOne( {
"$id": "h1961-01",
"location": {
"$db": "novels",
"$ref": "2007042768",
"$inc": true
} } )
An update which creates a new document during an upsert is treated as an 在upsert期间创建新文档的更新被视为insert rather than an update for field name validation. Upserts can accept dollar ($) prefixed fields. insert,而不是字段名验证的update。upsert可以接受以美元($)为前缀的字段。However, upserts are a special case and similar update operations may cause an error if the 然而,upsert是一种特殊情况,如果更新的match portion of the update selects an existing document.match部分选择了现有文档,类似的更新操作可能会导致错误。
This code sample has 此代码示例具有upsert: true so it will insert a new document if the collection doesn't already contain a document that matches the query term, { "date": "2021-07-07" }. upsert: true,因此如果集合中尚未包含与查询词{ "date": "2021-07-07" }匹配的文档,它将插入一个新文档。If this sample code matches an existing document, the update will fail since 如果此示例代码与现有文档匹配,则更新将失败,因为$hotel is dollar ($) prefixed.$hotel的前缀是美元($)。
db.expenses.updateOne(
{ "date": "2021-07-07" },
{ $set: {
"phone": 25.17,
"$hotel": 320.10
} },
{ upsert: true }
)Document Replacing Updates文档替换更新
Update operators either replace existing fields with new documents or else modify those fields. In cases where the update performs a replacement, dollar (更新运算符可以用新文档替换现有字段,也可以修改这些字段。在更新执行替换的情况下,不允许以美元($) prefixed fields are not permitted as top level field names.$)为前缀的字段作为顶级字段名。
Consider a document like考虑以下文档
{
"_id": "E123",
"address": {
"$number": 123,
"$street": "Elm Road"
},
"$rooms": {
"br": 2,
"bath": 1
}
}
You could use an update operator that replaces an existing document to modify the 您可以使用替换现有文档的更新运算符来修改address.$street field but you could not update the $rooms field that way.address.$street字段,但您无法以这种方式更新$rooms字段。
db.housing.updateOne(
{ "_id": "E123" },
{ $set: { "address.$street": "Elm Ave" } }
)
Use 使用$setField as part of an aggregation pipeline to update top level dollar ($) prefixed fields like $rooms.$setField作为聚合管道的一部分来更新顶级美元($)前缀字段,如$rooms。
Document Modifying Updates文档修改更新
When an update modifies, rather than replaces, existing document fields, dollar (当更新修改而不是替换现有文档字段时,美元($) prefixed fields can be top level field names. Subfields can be accessed directly, but you need a helper method to access the top level fields.$)前缀字段可以是顶级字段名。子字段可以直接访问,但您需要一个辅助方法来访问顶级字段。
Tip
Consider a collection with documents like this inventory record:考虑一个包含此类inventory记录文档的集合:
{
_id: ObjectId("610023ad7d58ecda39b8d161"),
"part": "AB305",
"$bin": 200,
"quantity": 100,
"pricing": { sale: true, "$discount": 60 }
}
The pricing.$discount subfield can be queried directly.pricing.$discount子字段可以直接查询。
db.inventory.findAndModify( {
query: { "part": { $eq: "AB305" } },
update: { $inc: { "pricing.$discount": 10 } }
} )
Use $getField and $literal to access the value of the top level $bin field.
db.inventory.findAndModify( {
query: { $expr: {
$eq: [ { $getField: { $literal: "$bin" } }, 200 ]
} },
update: { $inc: { "quantity": 10 } }
} )Updates Using Aggregation Pipelines使用聚合管道进行更新
Use 在$setField, $getField, and $literal in the $replaceWith stage to modify dollar ($) prefixed fields in an aggregation pipeline.$replaceWith阶段中使用$setField、$getField和$literal来修改聚合管道中以美元($)为前缀的字段。
Consider a collection of school records like:考虑一组学校记录,如:
db.school.insertOne (
{
"_id": 100001,
"$term": "fall",
"registered": true,
"grade": 4
} )
Create a new collection for the spring semester using a pipeline to update the dollar (使用管道更新以美元($) prefixed $term field.$)为前缀的$term字段,为春季学期创建一个新的集合。
db.school.aggregate( [
{ $match: { "registered": true } },
{ $replaceWith: {
$setField: {
field: { $literal: "$term" },
input: "$$ROOT",
value: "spring"
} } },
{ $out: "spring2022" }
] )