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

发布于 2025-01-06 23:49:52 字数 156 浏览 4 评论 0原文

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

"_id" : BinData(0,"e8MEnzZoFyMmD7WSHdNrFJyEk8M=")

“0”是什么意思?

The MongoDB shell prints binary data as a Base64-encoded string wrapped in what looks like a function call:

"_id" : BinData(0,"e8MEnzZoFyMmD7WSHdNrFJyEk8M=")

What does the "0" mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满天都是小星星 2025-01-13 23:49:52

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

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

> new BinData(2, "1234")
BinData(2,"1234")

来自 shell

help misc
b = new BinData(subtype,base64str)  create a BSON BinData value

您的情况下的 0 是 BSON 子类型

http://bsonspec.org/ #/specation

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

此线程上有类似问题

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

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

The BSON BinData datatype is represented via class BinData in the shell. Run help misc for more information.

> new BinData(2, "1234")
BinData(2,"1234")

from the shell

help misc
b = new BinData(subtype,base64str)  create a BSON BinData value

The 0 in your case is the BSON subtype

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

Similar question on this thread

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

旧伤慢歌 2025-01-13 23:49:52

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)

这些只是帮助器,以便反序列化器可以根据这些字节表示的内容以不同方式解释二进制数据除外

现在要了解示例为何错误,您会注意到调用 BinData(2, "1234") 不会存储表示字符串“1234”的二进制文件,原因有两个:

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

请参阅 bsonspec.org 了解更多信息。

Macrolinux is right but you have to be careful with his example as it will work but by accident.

The first argument to BinData() is the BSON binary subtype which, as has been mentioned is one of the following:

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

These are just helpers so that the deserializer can interpret the binary data differently depending on what those bytes represent except for the subtype 2 which is like the generic subtype but stores an int32 representing the length of the byte array as the first 4 bytes of data.

Now to see why the example is wrong you'll note that calling BinData(2, "1234") doesn't store the binary representing the string "1234" for two reasons:

  • The BinData function interprets that string as a base64 encoded string.
  • Type 2 would require that the first 4 bytes be an int32 containing the length of the byte array.

See bsonspec.org for more information.

ゃ懵逼小萝莉 2025-01-13 23:49:52

相信它们对应于BSON子类型

子类型 ::= "\x00" 二进制/通用
| “\x01”函数
| “\x02”二进制(旧)
| “\x03”UUID
| “\x05”MD5
| “\x80”用户定义

看起来 0 几乎总是一个有效的选择。

I believe they they correspond to the BSON subtypes:

subtype ::= "\x00" Binary / Generic
| "\x01" Function
| "\x02" Binary (Old)
| "\x03" UUID
| "\x05" MD5
| "\x80" User defined

Looking at that, it appears that 0 is almost always a valid choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文