翻译或纠错本页面

BSON 类型

BSON is a binary serialization format used to store documents and make remote procedure calls in MongoDB. The BSON specification is located at bsonspec.org.

BSON supports the following data types as values in documents. Each data type has a corresponding number and string alias that can be used with the $type operator to query documents by BSON type.

Type Number Alias Notes
Double 1 “double”  

字符串

2 “string”  

对象

3 “object”  

数组

4 “array”  

二进制数据

5 “binData”  

未定义

6 “undefined”

已过期。

ObjectId 7 “objectId”  
Boolean 8 “bool”  

日期

9 “date”  

10 “null”  

正则表达式

11 “regex”  
DBPointer 12 “dbPointer”

已过期。

JavaScript 13 “javascript”  

符号

14 “symbol”

已过期。

JavaScript (带范围)

15 “javascriptWithScope”  

32位整数

16 “int”  

时间戳

17 “timestamp”  

64位整数

18 “long”  
Decimal128 19 “decimal” New in version 3.4.
Min key -1 “minKey”  
Max key 127

在比较不同 BSON 类型的值时,MongoDB使用下面的比较顺序(从低到高):

 

To determine a field’s type, see 在 mongo Shell 中检查类型.

If you convert BSON to JSON, see the Extended JSON reference.

The following sections describe special considerations for particular BSON types.

ObjectId

ObjectIds are small, likely unique, fast to generate, and ordered. ObjectId values consists of 12-bytes, where the first four bytes are a timestamp that reflect the ObjectId’s creation, specifically:

  • a 4-byte value representing the seconds since the Unix epoch,
  • a 3-byte machine identifier,
  • MongoDB 在比较的过程中,把一些类型看成相等,例如,数值类型会在比较之前进行转化。

  • 日期对象排在时间戳对象之前。之前数据集时间戳对象一起进行排序。

MongoDB将会把不存在的字段当做一个空的BSON对象。这样的话,对文档 { }{ a: null }a 字段上进行排序,在排序顺序上将会看做相等。

在数组中,小于比较或者升序排列会比较数组中最小的元素,大于比较或者降序排列会比较数组中最大的元素。因此,在比较一个字段值为单元素数组(例如, [ 1 ] )与一个非数组字段(例如, 2 )时,比较的是 12 。一个空数组(例如, [ ])的比较会将空数组处理为小于 null 或缺失字段。

MongoDB clients should add an _id field with a unique ObjectId. Using ObjectIds for the _id field provides the following additional benefits:

  • in the mongo shell, you can access the creation time of the ObjectId, using the ObjectId.getTimestamp() method.

  • sorting on an _id field that stores ObjectId values is roughly equivalent to sorting by creation time.

    重要

    The relationship between the order of ObjectId values and generation time is not strict within a single second. If multiple systems, or multiple processes or threads on a single system generate values, within a single second; ObjectId values do not represent a strict insertion order. Clock skew between clients can also result in non-strict ordering even for values because client drivers generate ObjectId values.

参见

下面的部分描述了对特定BSON类型的特殊考量。

字符串

BSON字符串都是UTF-8编码。一般说来,每种编程语言的驱动程序在序列化和反序列化BSON的时候,都会从语言的字符串形式转化为UTF-8。这就使得使用BSON字符串简单存储大多数国际字符变为可能。 [1] 此外,MongoDB $regex 查询支持正则字符串中使用UTF-8。

[1]

对于使用UTF-8字符集的字符串,在字符串上使用 sort() 理论上来说都是正确的。然而,由于内部的 sort() 方法使用的是 C++的 strcmp api,排序时可能出错误处理某些字符串。

时间戳

BSON有一个特殊的时间戳类型用于 MongoDB 内部 使用,与普通的 日期 类型不相关。 时间戳值是一个64位的值。其中:

  • 前32位是一个 time_t 值(与Unix新纪元相差的秒数)

  • 后32位是在某秒中操作的一个递增的``序数``。

在单个 mongod 实例中,时间戳值通常是唯一的。

在复制集中, oplog 有一个 ts 字段。这个字段中的值使用BSON时间戳表示了操作时间。

注解

BSON时间戳类型主要用于 MongoDB*内部* 使用 。在大多数情况下的应用开发中,你可以使用BSON日期类型。查阅 日期 了解更多信息。

如果你插入一个在一级字段中包含空BSON时间戳的文档,MongoDB服务器将会使用当前的时间戳值替换空的时间戳。例如,如果你使用下面的操作插入一个包含时间戳值的文档:

var a = new Timestamp();

db.test.insert( { ts: a } );

然后, db.test.find() 操作将会返回一个类似于下面的文档:

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

如果 ts 是一个嵌入文档中的字段,服务器将会继续让其保持空的时间戳值。

在 2.6 版更改: 之前,服务器只会替换插入文档内前两个字段中(包括``_id``)的空时间戳值。现在,MongoDB将会对任何一级字段中的空时间戳进行替换。

日期

BSON日期是一个64位整数,表示当前距离Unix新纪元(1970年1月1日)的毫秒数。这样就保证了过去和未来2.9亿年的可表示日期范围。

正式的BSON说明 <http://bsonspec.org/#/specification>`_ 将BSON日期类型成为 UTC 时间

BSON日期类型是有符号的。 [2] 负数表示1970年之前的日期。

示例

Construct a Date using the new Date() constructor in the mongo shell:

var mydate1 = new Date()

示例

Construct a Date using the ISODate() constructor in the mongo shell:

var mydate2 = ISODate()

示例

Return the Date value as string:

mydate1.toString()

示例

Return the month portion of the Date value; months are zero-indexed, so that January is month 0:

mydate1.getMonth()
[2]

在2.0版本之前, 日期 值被错误地表示为 无符号整数,大大地影响了 日期 字段上的排序、范围查询以及索引。由于在升级过程中不会重建索引,所以如果您是在早期版本中在 日期 字段上创建的索引,请对与应用相关的、1970年前的日期进行重新索引。