Documents文档

On this page本页内容

MongoDB stores data records as BSON documents.MongoDB将数据记录存储为BSON文档。BSON is a binary representation of JSON documents, though it contains more data types than JSON.BSON是JSON文档的二进制表示,尽管它包含的数据类型比JSON多。For the BSON spec, see bsonspec.org.有关BSON规范,请参见bsonspec.org网站。See also BSON Types.另请参见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.字段的值可以是任何BSON数据类型,包括其他文档、数组和文档数组。For example, the following document contains values of varying types:例如,以下文档包含各种类型的值:

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:上述字段具有以下数据类型:

Field Names字段名称

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保留用作主键;其值在集合中必须是唯一的、不可变的,并且可以是数组以外的任何类型。
  • Field names cannot contain the null character.字段名不能包含null字符。
  • Top-level field names cannot start with the dollar sign ($) character.顶级字段名不能以美元符号($)字符开头。

    Otherwise, starting in MongoDB 3.6, the server permits storage of field names that contain dots (i.e. .) and dollar signs (i.e. $).否则,从MongoDB 3.6开始,服务器允许存储包含点(即.)和美元符号(即$)的字段名。

    Important

    The MongoDB Query Language cannot always meaningfully express queries over documents whose field names contain these characters (see SERVER-30575).MongoDB查询语言并非始终能够有意义地表达对字段名包含这些字符的文档的查询(请参阅SERVER-30575)。

    Until support is added in the query language, the use of $ and . in field names is not recommended and is not supported by the official MongoDB drivers.在查询语言中添加支持之前,不建议在字段名中使用$.,官方MongoDB驱动程序也不支持这种做法。

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.请参阅Maximum Index Key Length以了解详情。

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:有关查询数组的示例,请参见:

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"

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 MB。

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提供了GridFS API。See mongofiles and the documentation for your driver for more information about GridFS.有关GridFS的更多信息,请参阅mongofiles驱动程序文档。

Document Field Order文档字段顺序

MongoDB preserves the order of the document fields following write operations 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.包括重命名字段名的更新可能会导致文档中字段的重新排序。

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字段的文档,则服务器会将该字段移到开头。
  • The _id field may contain values of any BSON data type, other than an array._id字段可以包含除数组以外的任何BSON数据类型的值。

    Warning

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

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.为了更有效地存储集合和索引中的UUID值,请将UUID存储为BSONBinData类型的值。

    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 db.collection.update() operation.更新规范文档使用更新运算符指定在db.collection.update()操作过程中对特定字段执行的数据修改。

{
  <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:下载内容包括以下资源: