BSON Types

BSON Types

BSON 是一个二进制序列化格式,在MongoDB里面被用来做文档存储和远程程序调用。 BSON 规格说明位于 bsonspec.org.

每个BSON 类型都有数字和字符标识,如下表所列:

Type Number Alias Notes
Double 1 “double”  
String 2 “string”  
Object 3 “object”  
Array 4 “array”  
Binary data 5 “binData”  
Undefined 6 “undefined” Deprecated.
ObjectId 7 “objectId”  
Boolean 8 “bool”  
Date 9 “date”  
Null 10 “null”  
Regular Expression 11 “regex”  
DBPointer 12 “dbPointer” Deprecated.
JavaScript 13 “javascript”  
Symbol 14 “symbol” Deprecated.
JavaScript (with scope) 15 “javascriptWithScope”  
32-bit integer 16 “int”  
Timestamp 17 “timestamp”  
64-bit integer 18 “long”  
Decimal128 19 “decimal” New in version 3.4.
Min key -1 “minKey”  
Max key 127 “maxKey”  

你可以使用 $type 操作符和这些值根据BSON 类型来查询文档.  $type聚合操作符返回操作符表达式operator expression 使用的 已有BSON 类型字符.

想要确定一个属性的类型,查看 Check Types in the mongo Shell.

如果你想从 BSON 转换成JSON, 查看Extended JSON .

下列章节是特殊BSON 类型的注意事项.

ObjectId

ObjectIds 很小, 几乎是唯一的, 可以快速创建而且有序的. ObjectId 的值由12 字节组成, 前4个字节是一个反映ObjectId 创建时间的时间戳. Specifically:

  • a 4-byte 从Unix 纪元开始的秒数,
  • a 3-byte 机器ID标识,
  • a 2-byte 进程ID, and
  • a 3-byte 从一个随机数开始的计数器.

MongoDB中每个集合中的文档都需要一个唯一的 _id 作为主键 primary key.如果插入的文档省略了 _id 属性, MongoDB 会 自动创建一个 值为ObjectId的 _id属性 .

这同样适用于设置属性 upsert: true的更新操作.

MongoDB clients 应该为 _id 属性添加一个唯一的 ObjectId. 使用ObjectIds 作为 _id 属性有下列好处:

  • 在 mongo shell中, 你可以使用 ObjectId.getTimestamp() 方法获取ObjectId的创建时间.

  • 在使用ObjectId 的_id 属性上排序相当于在创建时间上排序 .

    IMPORTANT

    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.

SEE ALSO

ObjectId()

String

BSON 字符编码是 UTF-8. 通常情况下, 每种语言的驱动在序列化和反序列化BSON时都会转换成UTF-8编码. 这样,BSON 可以轻松存储大多数国际化字符. [1] 另外,MongoDB $regex 正则表达式查询中的正则字符串支持UTF-8.

[1] Given strings using UTF-8 character sets, using sort() on strings will be reasonably correct. However, because internally sort() uses the C++ strcmp api, the sort order may handle some characters incorrectly.

时间戳

BSON 有一个MongoDB内部使用的特殊时间戳类型 ,并且和常规的时间类型没关系Date. 时间戳的值是一个 64 位值:

  • 前32 位是 time_t (从Unix 纪元开始的秒数)
  • 后32 位是一个在某一秒内操作的可增长的序数.

在一个 mongod 实例中, 时间戳的值是唯一的.

在副本集中,  oplog 有一个ts 属性. 这个属性的值是BSON 时间戳类型,表示操作的时间.

NOTE

BSON 时间戳类型是MongoDB 内部使用. 大多数情况下,你可以使用 BSON日期类型. 更多信息查看Date .

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

var a = new Timestamp();

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

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

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

如果ts 是一个内嵌文档的属性, 服务器将会继续让其保持空的时间戳值.

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

Date

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

The official BSON specification refers to the BSON Date type as the UTC datetime.

BSON 日期类型有符号. [2] 负值表示 1970前的日期.

EXAMPLE

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

var mydate1 = new Date()

EXAMPLE

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

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:

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

猜你喜欢

转载自blog.csdn.net/qq_27623337/article/details/79203471