BSON 数组是异构的还是同质的?

Are BSON arrays heterogeneous or homogeneous?

这在the spec中没有明确说明,尽管脚注中的示例是针对同构数组给出的。

google 搜索未得出明确的答案。

查看各种 API,内容作为对象返回,而不是同类 value/type,然后可以对其进行动态检查。

我个人 可以 看到异构数组的唯一实际原因是,如果它们包含 document,它们可能具有不同的字段集。否则,用户会更喜欢(异质)document 而不是(同质)array

在规范中这样说。

它表示 文档 由以空字节为前缀的元素组成

document    ::=     int32 e_list "\x00"

一个文档由元素组成

e_list  ::=     element e_list

元素可以是 BSON 支持的任何类型

element     ::=     "\x01" e_name double    64-bit binary floating point
    |   "\x02" e_name string    UTF-8 string
    |   "\x03" e_name document  Embedded document
 ....<snip>....

页面底部的第一个注释解释了列表只是具有神奇的升序字符串键的文档。

Array - The document for an array is a normal BSON document with integer values for the keys, starting with 0 and continuing sequentially. For example, the array ['red', 'blue'] would be encoded as the document {'0': 'red', '1': 'blue'}.

因此, BSON 会愉快地序列化 {"Key1":[12, "12", 12.1, "a string", Binary(0x001232)]}