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类型。
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
name
first
and last
.first
和last
字段的嵌入文档。birth
and death
hold values of the Date type.birth
和death
保存日期类型的值。contribs
holds an array of strings.contribs
包含一个字符串数组。views
holds a value of the NumberLong type.views
包含NumberLong类型的值。Field names are strings.字段名是字符串。
Documents文档 have the following restrictions on field names:对字段名有以下限制:
_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
保留用作主键;它的值在集合中必须是唯一的、不可变的,并且可以是数组以外的任何类型。_id
contains subfields, the subfield names cannot begin with a ($
) symbol._id
包含子字段,则子字段名称不能以($
)符号开头。null
character.null
字符。.
) and dollar signs ($
)..
)的字段名美元符号($
)。$
) and (.
) in field names. $
和.
的支持。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进程不会向现有用户文档中添加重复的字段。
"4.0"
or earlierMongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document.MongoDB使用点记号法来访问数组的元素和嵌入式文档的字段。
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:有关查询数组的示例,请参阅:
$[]
$[<identifier>]
$
$
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" } }, ... }
last
in the name
field, use the dot notation "name.last"
.name
字段中的last
字段,请使用点记号法"name.last"
。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:有关查询嵌入文档的示例,请参阅:
Documents have the following attributes:文档具有以下属性:
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 有关GridFS的更多信息,请参阅mongofiles
and the documentation for your driver for more information about GridFS.mongofiles
和驱动程序的文档。
Unlike JavaScript objects, the fields in a BSON document are ordered.与JavaScript对象不同,BSON文档中的字段是有序的。
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:a
和b
进行比较时:
{a: 1, b: 1}
{a: 1, b: 1}
{a: 1, b: 1}
{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
。
For write operations, MongoDB preserves the order of the document fields except for the following cases:对于写操作,MongoDB保留文档字段的顺序,但以下情况除外:
_id
FieldIn 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
字段具有以下行为和约束:
_id
field during the creation of a collection._id
字段上创建唯一索引。_id
field is always the first field in the documents. _id
字段始终是文档中的第一个字段。_id
field first, then the server will move the field to the beginning._id
字段的文档,则服务器会将该字段移到开头。_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数据类型的值,而不是数组、正则表达式或未定义的值。
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
值的常用选项:
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
类型的索引键将更有效地存储在索引中:
Most MongoDB driver clients will include the 大多数MongoDB驱动程序客户端将包括_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
._id
字段,并在向MongoDB发送插入操作之前生成ObjectId
;但是,如果客户端发送的文档没有_id
字段,mongod
将添加_id
字段并生成ObjectId
。
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 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 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 define the field to index and the index type:索引规范文档定义要索引的字段和索引类型:
{ <field1>: <type1>, <field2>: <type2>, ... }
For more information on the MongoDB document model, download the MongoDB Application Modernization Guide.有关MongoDB文档模型的更多信息,请下载MongoDB应用程序现代化指南。
The download includes the following resources:下载内容包括以下资源: