MongoDB allows field names that contain dollar signs (MongoDB允许字段名包含美元符号($) or periods (.). However, use of these characters within field names is discouraged, because MongoDB does not support some features with these fields.$)或句点(.)。但是,不建议在字段名中使用这些字符,因为MongoDB不支持这些字段的某些功能。
In most cases data that has been stored using field names like these is not directly accessible. You need to use helper methods like 在大多数情况下,使用这样的字段名存储的数据不能直接访问。您需要在访问这些字段的查询中使用辅助方法,如$getField, $setField, and $literal in queries that access those fields.$getField、$setField和$literal。
The field name validation rules are not the same for all types of storage operations.并非所有类型的存储操作的字段名验证规则都是相同的。
General Restrictions一般限制
There are some general restrictions on using dollar (使用以美元($) prefixed field names or field names that contain a period (.). These fields cannot:$)为前缀的字段名或包含句点(.)的字段名有一些一般限制。这些字段不能:
Be indexed被编入索引Be used as part of a shard key用作分片键的一部分Be validated using使用$jsonSchema$jsonSchema进行验证Be modified with an escape sequence用逃逸序列进行修改Be used with Field Level Encryption与字段级加密一起使用Be used as a subfield in an用作_iddocument_id文档中的子字段Have more than 255 words separated by periods in field names字段名中有255个以上用句点分隔的单词
Warning
Possible Data Loss With Dollar Signs ($) and Periods (.)带有美元符号($)和点号(.)的可能数据丢失
There is a small chance of data loss when using dollar (在MongoDB 5.0之前的服务器上,如果使用美元($) prefixed field names or field names that contain periods (.) if these field names are used in conjunction with unacknowledged writes (write concern w=0) on servers that are older than MongoDB 5.0.$)前缀的字段名或包含句点(.)的字段名与未确认的写入(写入关注w=0)结合使用,则数据丢失的可能性很小。
When running 运行insert, update, and findAndModify commands, drivers that are 5.0 compatible remove restrictions on using documents with field names that are dollar ($) prefixed or that contain periods (.). insert、update和findAndModify命令时,与5.0兼容的驱动程序会取消对使用字段名以美元($)为前缀或包含点点(.)的文档的限制。These field names generated a client-side error in earlier driver versions.这些字段名在早期驱动程序版本中生成了客户端错误。
The restrictions are removed regardless of the server version the driver is connected to. If a 5.0 driver sends a document to an older server, the document will be rejected without sending an error.无论驱动程序连接到哪个服务器版本,这些限制都会被删除。如果5.0驱动程序将文档发送到较旧的服务器,则文档将被拒绝而不会发送错误。
Warning
Import and Export Concerns With Dollar Signs ($) and Periods (.)美元符号($)和点号(.)的导入导出问题
Starting in MongoDB 5.0, document field names can be dollar (从MongoDB 5.0开始,文档字段名可以以美元($) prefixed and can contain periods (.). $)为前缀,并且可以包含句点(.)。However, 然而,在某些使用这些字符的字段名的情况下,mongoimport and mongoexport may not work as expected in some situations with field names that make use of these characters.mongoimport和mongoexport可能无法按预期工作。
MongoDB Extended JSON v2 cannot differentiate between type wrappers and fields that happen to have the same name as type wrappers. MongoDB Extended JSON v2无法区分类型包装器和恰好与类型包装器同名的字段。Do not use Extended JSON formats in contexts where the corresponding BSON representations might include dollar (在相应的BSON表示可能包含美元($) prefixed keys. $)前缀键的上下文中,不要使用扩展JSON格式。The DBRef mechanism is an exception to this general rule.DBRef机制是此一般规则的例外。
There are also restrictions on using 在字段名中使用带有句点(mongoimport and mongoexport with periods (.) in field names. .)的mongoimport和mongoexport也有限制。Since CSV files use the period (由于CSV文件使用句点(.) to represent data hierarchies, a period (.) in a field name will be misinterpreted as a level of nesting..)来表示数据层次结构,字段名中的句点(.)将被误解为嵌套级别。
Learn More了解更多
For examples of how to handle field names that contain periods and dollar signs, see these pages:有关如何处理包含句点和美元符号的字段名的示例,请参阅以下页面: