Database Manual / CRUD Operations / CRUD Concepts / Periods & Dollar Signs

Field Names with Periods带句点的字段名

This section summarizes how to insert, query, and update documents with field names that contain a period. MongoDB discourages the use of field names that contain a period because some features aren't supported with these fields. 本节总结了如何插入、查询和更新字段名包含句点的文档。MongoDB不鼓励使用包含句点的字段名,因为这些字段不支持某些功能。See General Restrictions for more information.有关更多信息,请参阅一般限制

Note

Limit the number of words separated by periods in field names to less than 255. If you attempt to use more, MongoDB returns an error.将字段名中用句点分隔的单词数限制在255以内。如果您尝试使用更多,MongoDB将返回错误。

Insert a Field Name with a Period插入带句点的字段名

To insert a document that contains a field name with a period, put the field name in quotes.要插入包含带句点的字段名的文档,请将字段名放在引号中。

The following command inserts a document that contains a field name price.usd:以下命令插入包含字段名price.usd的文档:

db.inventory.insertOne(
{
"item" : "sweatshirt",
"price.usd": 45.99,
"quantity": 20
}
)

Query a Field that has a Period查询有句点的字段

To query for a field that has a period, use the $getField operator.要查询有句点的字段,请使用$getField运算符。

The following query returns documents where the price.usd field is greater than 40:以下查询返回price.usd字段大于40的文档:

db.inventory.find(
{
$expr:
{
$gt: [ { $getField: "price.usd" }, 40 ]
}
}
)
[
{
_id: ObjectId("66145f9bcb1d4abffd2f1b50"),
item: 'sweatshirt',
'price.usd': 45.99,
quantity: 20
}
]

If you don't use $getField, MongoDB treats the field name with a period as an embedded object. 如果不使用$getField,MongoDB会将带句点的字段名视为嵌入式对象。For example, the following query matches documents where a usd field inside of a price field is greater than 40:例如,以下查询匹配price字段内usd字段大于40的文档:

db.inventory.find( {
"price.usd": { $gt: 40 }
} )

The preceding query would match this document:前面的查询将与此文档匹配:

{
"item" : "sweatshirt",
"price": {
"usd": 45.99
},
"quantity": 20
}

Update a Field that has a Period更新具有点号的字段

To update a field that has a period, use an aggregation pipeline with the $setField operator.要更新具有句点的字段,请使用带有$setField运算符的聚合管道。

The following operation sets the price.usd field to 29.99:以下操作将price.usd字段设置为29.99

db.inventory.updateOne(
{ "item": "sweatshirt" },
[
{
$replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: 29.99
}
}
}
]
)

If you don't use $setField, MongoDB treats the field name with a period as an embedded object. For example, the following operation does not update the existing price.usd field, and instead inserts a new field usd, embedded inside of a price field:如果不使用$setField,MongoDB会将带句点的字段名视为嵌入式对象。例如,以下操作不会更新现有的price.usd字段,而是插入一个嵌入price字段内的新字段usd

db.inventory.updateOne(
{ "item": "sweatshirt" },
{ $set: { "price.usd": 29.99 } }
)

Resulting document:结果文件:

[
{
_id: ObjectId("66145f9bcb1d4abffd2f1b50"),
item: 'sweatshirt',
'price.usd': 45.99
quantity: 20,
price: { usd: 29.99 }
}
]

For more examples of updates with aggregation pipelines, see Updates with Aggregation Pipeline.有关聚合管道更新的更多示例,请参阅聚合管道更新

Learn More了解更多