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.orgSee 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. 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.birthdeath保存日期类型的值。
  • contribs holds an array of strings.contribs包含一个字符串数组
  • views holds a value of the NumberLong type.views包含NumberLong类型的值。

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保留用作主键;它的值在集合中必须是唯一的、不可变的,并且可以是数组以外的任何类型。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 earlier功能兼容版本(fCV)设置为“4.0”或更早版本的MongoDB 2.6到MongoDB版本
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"

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文档字段顺序

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.包括字段名的重命名更新可能会导致文档中字段的重新排序。

The _id Field

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驱动程序客户端将包括_id字段,并在向MongoDB发送插入操作之前生成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应用现代化记分卡
←  Shard a Time Series CollectionBSON Types →