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指定以下值之一:
| Number | |
|---|---|
| 0 | |
| 1 | |
| 2 | Binary (old) |
| 3 | UUID (old) |
| 4 | UUID |
| 5 | MD5 |
| 6 | |
| 7 |
|
| 8 | ###.###。 |
| 9 | |
| 128 | Custom 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()对象
BinData() ObjectUse 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()对象的长度
BinData() ObjectUse 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