Database Manual / Reference / mongosh Methods / Object Constructors

BinData() (mongosh method方法)

Definition定义

Creates a binary data object.创建二进制数据对象。

BinData has the following syntax:具有以下语法:

BinData(<sub_type>,<buffer>)
Parameter参数Type类型Description描述
sub_typeinteger整数The binary subtype二进制子类型
bufferstring字符串The buffer object containing binary data. Must be a base 64 encoded string value.包含二进制数据的缓冲区对象。必须是基64编码字符串值。
Returns:返回A binary data object.二进制数据对象。

Binary Subtypes二进制子类型

Specify one of the following values for sub_type:sub_type指定以下值之一:

NumberDescription描述
0Generic binary subtype通用二进制子类型
1Function data功能数据
2Binary (old)
3UUID (old)
4UUID
5MD5
6Encrypted BSON value加密的BSON值
7

Compressed time series data压缩时间序列数据

New in version 5.2.在版本5.2中新增。

8Sensitive data, such as a key or secret. MongoDB does not log literal values for binary data with subtype 8. Instead, MongoDB logs a placeholder value of ###.敏感数据,如键或秘密。MongoDB不会记录子类型为8的二进制数据的文字值。相反,MongoDB会记录一个占位符值###
9Vector data, which is densely packed arrays of numbers of the same type.矢量数据,它是由同一类型的数字组成的密集数组。
128Custom data

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Behavior行为

The endianness of your system depends on the architecture of your machine. 系统的端序取决于机器的架构。Numbers in BSON data are always stored as little-endian, if your system is big-endian this means that numeric data is converted between big and little endian.BSON数据中的数字始终以小端存储,如果系统是大端,这意味着数字数据在大端和小端之间转换。

In the context of the bit-test match expression operators:在比特测试匹配表达式运算符的上下文中:

BinData values act as bitmasks and are interpreted as though they are arbitrary-length unsigned little-endian numbers. The lowest-addressable byte is always interpreted as the least significant byte. BinData值充当位掩码,并被解释为任意长度的无符号小端数字。最低可寻址字节始终被解释为最低有效字节。Similarly, the highest-addressable byte in the BinData is always interpreted as the most significant byte.同样,BinData中最高可寻址字节始终被解释为最高有效字节。

Examples示例

Insert a BinData() Object插入BinData()对象

Use the BinData() constructor to create the bdata variable.使用BinData()构造函数创建bdata变量。

var bdata = BinData(0, "gf1UcxdHTJ2HQ/EGQrO7mQ==")

Insert the object into the testbin collection.将对象插入到testbin集合中。

db.testbin.insertOne( { _id : 1, bin_data: bdata } )

Query the testbin collection for the inserted document.查询插入文档的testbin集合。

db.testbin.find()

You can see the binary buffer stored in the collection.您可以看到存储在集合中的二进制buffer

{
_id: 1,
bin_data: Binary(Buffer.from("81fd547317474c9d8743f10642b3bb99", "hex"), 0)
}

Get the Length of BinData() Object获取BinData()对象的长度

Use the BinData() constructor to create the bdata variable.使用BinData()构造函数创建bdata变量。

var bdata = BinData(0, "gf1UcxdHTJ2HQ/EGQrO7mQ==")

Use .length() to return the bit length of the object.使用.length()返回对象的位长度。

bdata.length()

The returned value is:返回值为:

16