Docs HomeMongoDB Manual

BSON TypesBSON类型

BSON is a binary serialization format used to store documents and make remote procedure calls in MongoDB. 是一种二进制序列化格式,用于存储文档并在MongoDB中进行远程过程调用。The BSON specification is located at bsonspec.org.BSON规范位于bsonspec.org

Each BSON type has both integer and string identifiers as listed in the following table:每个BSON类型都有下表中列出的整数和字符串标识符:

Type类型Number数值Alias别名Notes备注
Double1"double"
String2"string"
Object3"object"
Array4"array"
Binary data5"binData"
Undefined6"undefined"Deprecated.已弃用。
ObjectId7"objectId"
Boolean8"bool"
Date9"date"
Null10"null"
Regular Expression11"regex"
DBPointer12"dbPointer"Deprecated.
JavaScript13"javascript"
Symbol14"symbol"Deprecated.
JavaScript code with scope15"javascriptWithScope"Deprecated in MongoDB 4.4.在MongoDB 4.4中已弃用
32-bit integer16"int"
Timestamp17"timestamp"
64-bit integer18"long"
Decimal12819"decimal"
Min key-1"minKey"
Max key127"maxKey"
  • The $type operator supports using these values to query fields by their BSON type. $type运算符支持使用这些值按字段的BSON类型查询字段。$type also supports the number alias, which matches the integer, decimal, double, and long BSON types.还支持number别名,它匹配BSON类型integerdecimaldoublelong
  • The $type aggregation operator returns the BSON type of its argument.$type聚合运算符返回其参数的BSON类型。
  • The $isNumber aggregation operator returns true if its argument is a BSON integer, decimal, double, or long. 如果$isNumber聚合运算符的参数是BSONintegerdecimaldoublelong,则返回trueNew in version 4.4

To determine a field's type, see Type Checking.要确定字段的类型,请参阅类型检查

If you convert BSON to JSON, see the Extended JSON reference.如果您将BSON转换为JSON,请参阅Extended JSON参考

The following sections describe special considerations for particular BSON types.以下各节介绍了特定BSON类型的特殊注意事项。

Binary Data二进制数据

A BSON binary binData value is a byte array. BSON二进制binData值是一个字节数组。A binData value has a subtype that indicates how to interpret the binary data. binData值具有指示如何解释二进制数据的子类型。The following table shows the subtypes.下表显示了子类型。

Number数值Subtype
0Generic binary subtype通用二进制子类型
1Function data功能数据
2Binary (old)
3UUID (old)
4UUID
5MD5
6Encrypted BSON value加密的BSON值
7Compressed time series data 压缩的时间序列数据New in version 5.2. 5.2版新增。
128Custom data自定义数据

ObjectId

ObjectIds are small, likely unique, fast to generate, and ordered. ObjectId values are 12 bytes in length, consisting of:ObjectId很小,可能是唯一的,生成速度快,而且有序。ObjectId值的长度为12个字节,包括:

  • A 4-byte timestamp, representing the ObjectId's creation, measured in seconds since the Unix epoch.一个4字节的时间戳,表示ObjectId的创建,自Unix纪元以来以秒为单位测量。
  • A 5-byte random value generated once per process. This random value is unique to the machine and process.每个进程生成一次的5字节随机值。该随机值对于机器和进程是唯一的。
  • A 3-byte incrementing counter, initialized to a random value.一种3字节递增计数器,初始化为随机值。

For timestamp and counter values, the most significant bytes appear first in the byte sequence (big-endian). 对于时间戳和计数器值,最高有效的字节首先出现在字节序列中(big-endian)。This is unlike other BSON values, where the least significant bytes appear first (little-endian).这与其他BSON值不同,在BSON值中,最低有效字节首先出现(little-endian)。

If an integer value is used to create an ObjectId, the integer replaces the timestamp.如果使用整数值创建ObjectId,则该整数将替换时间戳。

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的更新操作插入的文档。

MongoDB clients should add an _id field with a unique ObjectId. Using ObjectIds for the _id field provides the following additional benefits:MongoDB客户端应该添加一个具有唯一ObjectId的_id字段。将ObjectId用于_id字段提供了以下额外好处:

  • in mongosh, you can access the creation time of the ObjectId, using the ObjectId.getTimestamp() method.mongosh中,可以使用ObjectId.getTimestamp()方法访问ObjectId的创建时间。
  • sorting on an _id field that stores ObjectId values is roughly equivalent to sorting by creation time.对存储ObjectId值的_id字段进行排序大致相当于按创建时间进行排序。

    Important

    While ObjectId values should increase over time, they are not necessarily monotonic. This is because they:虽然ObjectId值应该随着时间的推移而增加,但它们不一定是单调的。这是因为他们:

    • Only contain one second of temporal resolution, so ObjectId values created within the same second do not have a guaranteed ordering, and只包含一秒钟的时间分辨率,因此在同一秒钟内创建的ObjectId值没有保证的顺序,并且
    • Are generated by clients, which may have differing system clocks.由客户端生成,客户端可能具有不同的系统时钟。

Use the ObjectId() methods to set and retrieve ObjectId values.使用ObjectId()方法可以设置和检索ObjectId值。

Starting in MongoDB 5.0, mongosh replaces the legacy mongo shell. 从MongoDB 5.0开始,mongosh取代了传统的mongoshell。The ObjectId() methods work differently in mongosh than in the legacy mongo shell. ObjectId()方法在mongosh中的工作方式与在传统mongoshell中的不同。For more information on the legacy methods, see Legacy mongo Shell.有关遗留方法的更多信息,请参阅legacy mongo Shell

String

BSON strings are UTF-8. In general, drivers for each programming language convert from the language's string format to UTF-8 when serializing and deserializing BSON. BSON字符串为UTF-8。通常,在序列化和反序列化BSON时,每种编程语言的驱动程序都会从该语言的字符串格式转换为UTF-8。This makes it possible to store most international characters in BSON strings with ease. 这使得可以轻松地将大多数国际字符存储在BSON字符串中。[1] In addition, MongoDB $regex queries support UTF-8 in the regex string.此外,MongoDB$regex查询在正则表达式字符串中支持UTF-8。

[1] Given strings using UTF-8 character sets, using sort() on strings will be reasonably correct. 给定使用UTF-8字符集的字符串,对字符串使用sort()是合理正确的。However, because internally sort() uses the C++ strcmp api, the sort order may handle some characters incorrectly.但是,由于内部sort()使用C++strcmp api,排序顺序可能会错误地处理某些字符。

Timestamps

BSON has a special timestamp type for internal MongoDB use and is not associated with the regular Date type. BSON有一个特殊的时间戳类型供MongoDB内部使用,并且与常规Date类型无关。This internal timestamp type is a 64 bit value where:此内部时间戳类型是一个64位值,其中:

  • the most significant 32 bits are a time_t value (seconds since the Unix epoch)最高有效的32位是time_t值(自Unix纪元以来的秒)
  • the least significant 32 bits are an incrementing ordinal for operations within a given second.最低有效位32位是用于给定秒内的操作的递增ordinal

While the BSON format is little-endian, and therefore stores the least significant bits first, the mongod instance always compares the time_t value before the ordinal value on all platforms, regardless of endianness.虽然BSON格式是小端序,因此首先存储最低有效位,但在所有平台上,mongod实例总是将time_t值与ordinal值进行比较,而不考虑端序。

Within a single mongod instance, timestamp values are always unique.在单个mongod实例中,时间戳值总是唯一的。

In replication, the oplog has a ts field. 在复制中,oplog有一个ts字段。The values in this field reflect the operation time, which uses a BSON timestamp value.此字段中的值反映操作时间,该操作时间使用BSON时间戳值。

Note

The BSON timestamp type is for internal MongoDB use. BSON时间戳类型供MongoDB内部使用。For most cases, in application development, you will want to use the BSON date type. See Date for more information.在大多数情况下,在应用程序开发中,您将希望使用BSON日期类型。有关详细信息,请参阅Date

When inserting a document that contains top-level fields with empty timestamp values, MongoDB replaces the empty timestamp values with the current timestamp value, with the following exception. 当插入包含具有空时间戳值的顶级字段的文档时,MongoDB会用当前时间戳值替换空的时间戳值,但以下情况除外。If the _id field itself contains an empty timestamp value, it will always be inserted as is and not replaced.如果_id字段本身包含一个空的时间戳值,则它将始终按原样插入,而不会被替换。

Example

Insert a document with an empty timestamp value:插入时间戳值为空的文档:

db.test.insertOne( { ts: new Timestamp() } );

Running db.test.find() would then return a document which resembles the following:运行db.test.find()将返回一个类似于以下内容的文档:

{ "_id" : ObjectId("542c2b97bac0595474108b48"), "ts" : Timestamp(1412180887, 1) }

The server has replaced the empty timestamp value for ts with the timestamp value at time of insert.服务器已将ts的空时间戳值替换为插入时的时间戳值。

Date

BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). BSON日期是一个64位整数,表示自Unix纪元(1970年1月1日)以来的毫秒数。This results in a representable date range of about 290 million years into the past and future.这导致了过去和未来约2.9亿年的可代表日期范围。

The official BSON specification refers to the BSON Date type as the UTC datetime.BSON官方规范将BSON日期类型称为UTC日期时间。

BSON Date type is signed. [2] Negative values represent dates before 1970.BSON日期类型已签名。[2] 负值表示1970年之前的日期。

Example

Construct a Date using the new Date() constructor in mongosh:使用mongosh中的new Date()构造函数构造Date:

var mydate1 = new Date()
Example

Construct a Date using the ISODate() constructor in mongosh:使用mongosh中的ISODate()构造函数构造日期:

var mydate2 = ISODate()
Example

Return the Date value as string:以字符串形式返回日期值:

mydate1.toString()
Example

Return the month portion of the Date value; months are zero-indexed, so that January is month 0:返回Date值的月份部分;月份为零索引,因此一月为0月份:

mydate1.getMonth()
[2] Prior to version 2.0, Date values were incorrectly interpreted as unsigned integers, which affected sorts, range queries, and indexes on Date fields. 在2.0版本之前,Date值被错误地解释为无符号整数,这影响了Date字段的排序、范围查询和索引。Because indexes are not recreated when upgrading, please re-index if you created an index on Date values with an earlier version, and dates before 1970 are relevant to your application.由于升级时不会重新创建索引,因此如果您使用早期版本在Date值上创建了索引,并且1970年之前的日期与您的应用程序相关,请重新创建索引。