On this page本页内容
BSON is a binary serialization format used to store documents and make remote procedure calls in MongoDB.BSON是一种二进制序列化格式,用于在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类型都有整数和字符串标识符,如下表所示:
1 | “double” | ||
2 | “string” | ||
3 | “object” | ||
4 | “array” | ||
5 | “binData” | ||
Undefined | 6 | “undefined” | |
ObjectId | 7 | “objectId” | |
8 | “bool” | ||
9 | “date” | ||
Null | 10 | “null” | |
11 | “regex” | ||
DBPointer | 12 | “dbPointer” | |
JavaScript | 13 | “javascript” | |
14 | “symbol” | ||
15 | “javascriptWithScope” | ||
16 | “int” | ||
17 | “timestamp” | ||
18 | “long” | ||
Decimal128 | 19 | “decimal” | |
Min key | -1 | “minKey” | |
Max key | 127 | “maxKey” |
$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.$type
还支持数字别名,它与integer、decimal、double和long BSON类型匹配。$type
aggregation operator returns the BSON type of its argument.$type
聚合运算符返回其参数的BSON类型。$isNumber
aggregation operator returns true
if its argument is a BSON integer, decimal, double, or long.$isNumber
聚合运算符的参数是BSON integer、decimal、double或long类型的,则返回true
。To determine a field’s type, see Check Types in the mongo Shell.要确定字段的类型,请参阅mongo Shell中检查类型。
If you convert BSON to JSON, see the Extended JSON reference.如果要将BSON转换为JSON,请参阅扩展的JSON。
The following sections describe special considerations for particular BSON types.以下各节描述了特定BSON类型的特殊注意事项。
ObjectIds are small, likely unique, fast to generate, and ordered.Objectid很小,可能是唯一的,生成速度很快,而且是有序的。ObjectId values are 12 bytes in length, consisting of:ObjectId值的长度为12字节,包括:
While the BSON format itself is little-endian, the timestamp and counter values are big-endian, with the most significant bytes appearing first in the byte sequence.虽然BSON格式本身是小端,但时间戳和计数器值是大端,最重要的字节首先出现在字节序列中。
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 MongoDB客户端应该添加一个带有唯一ObjectId的_id
field with a unique ObjectId._id
字段。
Using ObjectIds for the 将objectID用于_id
field provides the following additional benefits:_id
字段可提供以下附加好处:
mongo
shell, you can access the creation time of the ObjectId
, using the ObjectId.getTimestamp()
method.mongo
shell中,可以使用ObjectId.getTimestamp()
方法访问ObjectId
的创建时间。_id
field that stores ObjectId
values is roughly equivalent to sorting by creation time.ObjectId
值的_id
字段上排序大致相当于按创建时间排序。
See also另请参阅
BSON strings are UTF-8.BSON字符串是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。This makes it possible to store most international characters in BSON strings with ease.这使得在BSON字符串中轻松存储大多数国际字符成为可能。[1] In addition, MongoDB 此外,MongoDB $regex
queries support UTF-8 in the regex string.$regex
查询在regex字符串中支持UTF-8。
[1] | sort() on strings will be reasonably correct.sort() 将是相当正确的。sort() uses the C++ strcmp api, the sort order may handle some characters incorrectly.sort() 使用C++ strcmp API,排序顺序可能会处理一些字符错误。 |
BSON has a special timestamp type for internal MongoDB use and is not associated with the regular Date type.BSON有一个特殊的时间戳类型供内部MongoDB使用,并且与常规日期类型没有关联。This internal timestamp type is a 64 bit value where:此内部时间戳类型是64位值,其中:
time_t
value (seconds since the Unix epoch)time_t
值(自Unix纪元起的秒数)ordinal
for operations within a given second.ordinal
。While the BSON format is little-endian, and therefore stores the least significant bits first, the 虽然BSON格式本身是小端,因此首先存储最低有效位,但mongod
instance always compares the time_t
value before the ordinal
value on all platforms, regardless of endianness.mongod
实例总是在所有平台上比较time_t
值,然后比较ordinal
值,而不管字节序是多少。
Within a single 在单个mongod
instance, timestamp values are always unique.mongod
实例中,时间戳值总是唯一的。
In replication, the oplog has a 在副本中,oplog有一个ts
field.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.对于大多数情况,在应用程序开发中,您将希望使用BSON日期类型。See Date for more information.有关详细信息,请参见日期。
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:插入时间戳值为空的文档:
Running 运行db.test.find()
would then return a document which resembles the following:db.test.find()
将返回一个类似以下内容的文档:
The server has replaced the empty timestamp value for 服务器已将ts
with the timestamp value at time of insert.ts
的空时间戳值替换为插入时的时间戳值。
BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970).BSON Date是一个64位整数,表示自Unix epoch(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 datetime。
BSON Date type is signed.BSON日期类型是有符号的。[2] Negative values represent dates before 1970.负值表示1970年之前的日期。
Example
Construct a Date using the 在new Date()
constructor in the mongo
shell:mongo
shell中使用new Date()
构造函数构造日期:
Example
Construct a Date using the 在ISODate()
constructor in the mongo
shell:mongo
shell中使用ISODate()
构造函数构造日期:
Example
Return the 以字符串形式返回Date
value as string:Date
值:
Example
Return the month portion of the Date value; months are zero-indexed, so that January is month 返回日期值的月份部分;月份是零索引的,因此一月的月份值是0
:0
:
[2] | Date values were incorrectly interpreted as unsigned integers, which affected sorts, range queries, and indexes on Date fields.Date values with an earlier version, and dates before 1970 are relevant to your application.Date 值索引,并且1970年之前的日期与您的应用程序相关,请重新编制索引。 |