MongoDB的BinData中的"0"是什么意思(0,"e8MEnzZoFyMmD7WSHdNrFJyEk8M =")?

Thi*_*ilo 15 base64 mongodb bson

MongoDB shell将二进制数据打印为Base64编码的字符串,包含在看起来像函数调用的内容中:

"_id" : BinData(0,"e8MEnzZoFyMmD7WSHdNrFJyEk8M=")
Run Code Online (Sandbox Code Playgroud)

"0"是什么意思?

小智 18

http://docs.mongodb.org/manual/reference/mongodb-extended-json/#binary

BSON BinData数据类型通过shell中的类BinData表示.运行help misc以获取更多信息.

> new BinData(2, "1234")
BinData(2,"1234")
Run Code Online (Sandbox Code Playgroud)

从壳

help misc
b = new BinData(subtype,base64str)  create a BSON BinData value
Run Code Online (Sandbox Code Playgroud)

0你的情况是BSON亚型

http://bsonspec.org/#/specification

binary  ::=   int32 subtype (byte*)   Binary - The int32 is the number of bytes in the (byte*).
subtype ::=   "\x00"  Generic binary subtype
  |   "\x01"  Function
  |   "\x02"  Binary (Old)
  |   "\x03"  UUID (Old)
  |   "\x04"  UUID
  |   "\x05"  MD5
  |   "\x80"  User defined
Run Code Online (Sandbox Code Playgroud)

关于这个帖子的类似问题

http://groups.google.com/group/mongodb-dev/browse_thread/thread/1965aa234aa3ef1e


Tyl*_*ock 7

Macrolinux是对的,但你必须小心他的例子,因为它会起作用,但偶然.

BinData()的第一个参数是BSON二进制子类型,如上所述,它是以下之一:

generic:  \x00 (0)
function: \x01 (1)
old:      \x02 (2)
uuid_old: \x03 (3)
uuid:     \x04 (4)
md5:      \x05 (5)
user:     \x80 (128)
Run Code Online (Sandbox Code Playgroud)

这些只是帮助器,因此解串器可以根据这些字节所代表的内容不同地解释二进制数据,除了子类型2,它类似于通用子类型,但存储一个int32,表示字节数组的长度作为数据的前4个字节.

现在看看为什么示例是错误的,你会注意到调用BinData(2,"1234")并不存储表示字符串"1234"的二进制文件,原因有两个:

  • BinData函数将该字符串解释为base64编码的字符串.
  • 类型2要求前4个字节是包含字节数组长度的int32.

有关更多信息,请参阅bsonspec.org.