Docs HomeMongoDB Manual

Documents文件

MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, though it contains more data types than JSON. MongoDB将数据记录存储为BSON文档。BSON是JSON文档的二进制表示,尽管它包含的数据类型比JSON多。For the BSON spec, see bsonspec.org. See also BSON Types.有关BSON等级库,请参阅bsonspec.org。另请参见BSON类型

A MongoDB document.

Document Structure文档结构

MongoDB documents are composed of field-and-value pairs and have the following structure:MongoDB文档由字段和值对组成,具有以下结构:

{
field1: value1,
field2: value2,
field3: value3,
...
fieldN: valueN
}

The value of a field can be any of the BSON data types, including other documents, arrays, and arrays of documents. For example, the following document contains values of varying types:字段的值可以是任何BSON数据类型,包括其他文档、数组和文档数组。例如,以下文档包含不同类型的值:

var mydoc = {
_id: ObjectId("5099803df3f4948bd2f98391"),
name: { first: "Alan", last: "Turing" },
birth: new Date('Jun 23, 1912'),
death: new Date('Jun 07, 1954'),
contribs: [ "Turing machine", "Turing test", "Turingery" ],
views : NumberLong(1250000)
}

The above fields have the following data types:上述字段具有以下数据类型:

  • _id holds an ObjectId.保存了一个ObjectId
  • name holds an embedded document that contains the fields first and last.保存了一个嵌入的文档,该文档包含字段firstlast
  • birth and death hold values of the Date type.保存了日期类型的值。
  • contribs holds an array of strings.保存了一个字符串数组
  • views holds a value of the NumberLong type.保存了一个NumberLong类型的值。

Field Name字段名称s

Field names are strings.字段名称是字符串。

Documents文件 have the following restrictions on field names:对字段名称有以下限制:

  • The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array. 字段名称_id被保留用作主键;它的值在集合中必须是唯一的,是不可变的,并且可以是数组以外的任何类型。If the _id contains subfields, the subfield names cannot begin with a ($) symbol.如果_id包含子字段,则子字段名称不能以($)符号开头。
  • Field names cannot contain the null character.字段名称不能包含null字符。
  • The server permits storage of field names that contain dots (.) and dollar signs ($).服务器允许存储包含点(.)和美元符号($)的字段名。
  • MongodB 5.0 adds improved support for the use of ($) and (.) in field names. MongodB 5.0增加了对字段名称中使用($)和(.)的改进支持。There are some restrictions. 有一些限制。See Field Name Considerations for more details.有关更多详细信息,请参阅字段名称注意事项

BSON documents may have more than one field with the same name. BSON文档可能有多个具有相同名称的字段。Most MongoDB interfaces, however, represent MongoDB with a structure (e.g. a hash table) that does not support duplicate field names. 然而,大多数MongoDB接口使用不支持重复字段名的结构(例如哈希表)来表示MongoDB。If you need to manipulate documents that have more than one field with the same name, see the driver documentation for your driver.如果需要操作具有多个同名字段的文档,请参阅驱动程序的驱动程序文档

Some documents created by internal MongoDB processes may have duplicate fields, but no MongoDB process will ever add duplicate fields to an existing user document.内部MongoDB进程创建的一些文档可能有重复的字段,但任何MongoDB进程都不会向现有用户文档添加重复的字段。

Field Value Limit字段值限制

MongoDB 2.6 through MongoDB versions with featureCompatibilityVersion (fCV) set to "4.0" or earlierMongoDB 2.6至MongoDB版本,featureCompatibilityVersion(fCV)设置为“4.0”或更早版本
For indexed collections, the values for the indexed fields have a Maximum Index Key Length. See Maximum Index Key Length for details.对于索引集合,索引字段的值具有最大索引键长度。有关详细信息,请参阅最大索引键长度

Dot Notation点符号

MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document.MongoDB使用点表示法来访问数组的元素和嵌入文档的字段。

Arrays数组

To specify or access an element of an array by the zero-based index position, concatenate the array name with the dot (.) and zero-based index position, and enclose in quotes:要通过从零开始的索引位置指定或访问数组的元素,请将数组名称与点(.)和从零开始索引位置连接起来,并用引号括起来:

"<array>.<index>"

For example, given the following field in a document:例如,给定文档中的以下字段:

{
...
contribs: [ "Turing machine", "Turing test", "Turingery" ],
...
}

To specify the third element in the contribs array, use the dot notation "contribs.2".要指定contribs数组中的第三个元素,请使用点号"contribs.2"

For examples querying arrays, see:有关查询数组的示例,请参阅:

Tip

See also: 另请参阅:

  • $[] all positional operator for update operations,用于更新操作的所有位置运算符,
  • $[<identifier>] filtered positional operator for update operations,用于更新操作的经筛选的位置运算符,
  • $ positional operator for update operations,用于更新操作的位置运算符,
  • $ projection operator when array index position is unknown数组索引位置未知时的投影运算符
  • Query an Array查询数组 for dot notation examples with arrays.用于数组的点表示法示例。

Embedded Documents嵌入式文档

To specify or access a field of an embedded document with dot notation, concatenate the embedded document name with the dot (.) and the field name, and enclose in quotes:要使用句点表示法指定或访问嵌入文档的字段,请将嵌入文档名称与句点(.)和字段名称连接起来,并用引号括起来:

"<embedded document>.<field>"

For example, given the following field in a document:例如,给定文档中的以下字段:

{
...
name: { first: "Alan", last: "Turing" },
contact: { phone: { type: "cell", number: "111-222-3333" } },
...
}
  • To specify the field named last in the name field, use the dot notation "name.last".要指定name字段中命名字段last,请使用点表示法"name.last"
  • To specify the number in the phone document in the contact field, use the dot notation "contact.phone.number".要在contact字段中指定phone文档中的number,请使用点表示法"contact.phone.number"
Warning

Partition fields cannot use field names that contain a dot (.).分区字段不能使用包含句点(.)的字段名。

For examples querying embedded documents, see:有关查询嵌入文档的示例,请参阅:

Document Limitations文档限制

Documents have the following attributes:文档具有以下属性:

Document Size Limit文档大小限制

The maximum BSON document size is 16 megabytes.BSON文档的最大大小为16兆字节。

The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. 最大文档大小有助于确保单个文档不会使用过多的RAM,或者在传输过程中不会使用过大的带宽。To store documents larger than the maximum size, MongoDB provides the GridFS API. 为了存储大于最大大小的文档,MongoDB提供了GridFSneneneba API。See mongofiles and the documentation for your driver for more information about GridFS.有关GridFS的更多信息,请参阅mongofiles驱动程序的文档。

Document Field Order文档字段顺序

Unlike JavaScript objects, the fields in a BSON document are ordered.与JavaScript对象不同,BSON文档中的字段是按顺序排列的。

Field Order in Queries查询中的字段顺序

For queries, the field order behavior is as follows:对于查询,字段顺序行为如下:

  • When comparing documents, field ordering is significant. 在比较文档时,字段顺序非常重要。For example, when comparing documents with fields a and b in a query:例如,将文档与查询中的字段ab进行比较时:

    • {a: 1, b: 1} is equal to {a: 1, b: 1}
    • {a: 1, b: 1} is not equal to {b: 1, a: 1}
  • For efficient query execution, the query engine may reorder fields during query processing. 为了有效地执行查询,查询引擎可以在查询处理期间对字段进行重新排序。Among other cases, reordering fields may occur when processing these projection operators: $project, $addFields, $set, and $unset.在其他情况下,在处理以下投影运算符时可能会对字段进行重新排序:$project$addFields$set$unset

    • Field reordering may occur in intermediate results as well as the final results returned by a query.字段重新排序可能发生在中间结果以及查询返回的最终结果中。
    • Because some operations may reorder fields, you should not rely on specific field ordering in the results returned by a query that uses the projection operators listed earlier.由于某些操作可能会对字段重新排序,因此在使用前面列出的投影运算符的查询返回的结果中,不应依赖特定字段的排序。

Field Order in Write Operations写入操作中的字段顺序

For write operations, MongoDB preserves the order of the document fields except for the following cases:对于写操作,MongoDB保留文档字段的顺序,但以下情况除外:

  • The _id field is always the first field in the document._id字段始终是文档中的第一个字段。
  • Updates that include renaming of field names may result in the reordering of fields in the document.包括重命名字段名称(renaming)的更新可能会导致文档中字段的重新排序。

The _id Field_id字段

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. 在MongoDB中,存储在集合中的每个文档都需要一个唯一的_id字段作为主键If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.如果插入的文档省略了_id字段,MongoDB驱动程序会自动为_id字段生成一个ObjectId

This also applies to documents inserted through update operations with upsert: true.这也适用于通过upsert:true的更新操作插入的文档。

The _id field has the following behavior and constraints:_id字段具有以下行为和约束:

  • By default, MongoDB creates a unique index on the _id field during the creation of a collection.默认情况下,MongoDB在创建集合的过程中会在_id字段上创建一个唯一的索引。
  • The _id field is always the first field in the documents. _id字段始终是文档中的第一个字段。If the server receives a document that does not have the _id field first, then the server will move the field to the beginning.如果服务器首先接收到一个没有_id字段的文档,那么服务器将把该字段移到开头。

If the _id contains subfields, the subfield names cannot begin with a ($) symbol.如果_id包含子字段,则子字段名称不能以($)符号开头。

  • The _id field may contain values of any BSON data type, other than an array, regex, or undefined._id字段可以包含除数组、正则表达式或未定义之外的任何BSON数据类型的值。

    Warning

    To ensure functioning replication, do not store values that are of the BSON regular expression type in the _id field.为确保复制正常运行,请不要在_id字段中存储BSON正则表达式类型的值。

The following are common options for storing values for _id:以下是用于存储_id值的常见选项:

  • Use an ObjectId.使用ObjectId
  • Use a natural unique identifier, if available. 如果可用,请使用自然的唯一标识符。This saves space and avoids an additional index.这样可以节省空间并避免额外的索引。
  • Generate an auto-incrementing number.生成一个自动递增的数字。
  • Generate a UUID in your application code. 在应用程序代码中生成UUID。For a more efficient storage of the UUID values in the collection and in the _id index, store the UUID as a value of the BSON BinData type.为了在集合和_id索引中更有效地存储UUID值,请将UUID存储为BSON BinData类型的值。

    Index keys that are of the BinData type are more efficiently stored in the index if:如果出现以下情况,BinData类型的索引键将更有效地存储在索引中:

    • the binary subtype value is in the range of 0-7 or 128-135, and二进制子类型值在0-7或128-135的范围内,并且
    • the length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, or 32.字节数组的长度为:0、1、2、3、4、5、6、7、8、10、12、14、16、20、24或32。
  • Use your driver's BSON UUID facility to generate UUIDs. 使用驾驶员的BSON UUID功能生成UUID。Be aware that driver implementations may implement UUID serialization and deserialization logic differently, which may not be fully compatible with other drivers. 请注意,驱动程序实现可能以不同的方式实现UUID序列化和反序列化逻辑,这可能与其他驱动程序不完全兼容。See your driver documentation for information concerning UUID interoperability.有关UUID互操作性的信息,请参阅驱动程序文档
Note

Most MongoDB driver clients will include the _id field and generate an ObjectId before sending the insert operation to MongoDB; however, if the client sends a document without an _id field, the mongod will add the _id field and generate the ObjectId.大多数MongoDB驱动程序客户端在向MongoDB发送插入操作之前,都会包含_id字段并生成ObjectId;然而,如果客户端发送的文档没有_id字段,mongod将添加_id字段并生成ObjectId

Other Uses of the Document Structure文档结构的其他用途

In addition to defining data records, MongoDB uses the document structure throughout, including but not limited to: query filters, update specifications documents, and index specification documents除了定义数据记录外,MongoDB还始终使用文档结构,包括但不限于:查询筛选器更新规范文档索引规范文档

Query Filter Documents查询筛选文档

Query filter documents specify the conditions that determine which records to select for read, update, and delete operations.查询筛选文档指定条件,这些条件决定要选择哪些记录进行读取、更新和删除操作。

You can use <field>:<value> expressions to specify the equality condition and query operator expressions.您可以使用<field>:<value>表达式来指定相等条件和查询运算符表达式。

{
<field1>: <value1>,
<field2>: { <operator>: <value> },
...
}

For examples, see:有关示例,请参阅:

Update Specification Documents更新规范文档

Update specification documents use update operators to specify the data modifications to perform on specific fields during an update operation.更新规范文档使用更新运算符指定在更新操作期间要对特定字段执行的数据修改。

{
<operator1>: { <field1>: <value1>, ... },
<operator2>: { <field2>: <value2>, ... },
...
}

For examples, see Update specifications.有关示例,请参阅更新规范

Index Specification Documents索引规范文件

Index specification documents define the field to index and the index type:索引规范文档定义了要索引的字段和索引类型:

{ <field1>: <type1>, <field2>: <type2>, ...  }

Further Reading进一步阅读

For more information on the MongoDB document model, download the MongoDB Application Modernization Guide.有关MongoDB文档模型的更多信息,请下载MongoDB应用程序现代化指南

The download includes the following resources:下载包括以下资源:

  • Presentation on the methodology of data modeling with MongoDB介绍使用MongoDB进行数据建模的方法
  • White paper covering best practices and considerations for migrating to MongoDB from an RDBMS data model白皮书介绍了从RDBMS数据模型迁移到MongoDB的最佳实践和注意事项
  • Reference MongoDB schema with its RDBMS equivalent参考MongoDB模式及其RDBMS等价物
  • Application Modernization scorecard应用程序现代化记分卡