BinData()
On this page
Definition
Creates a binary data object.
BinData
has the following syntax:
BinData(<sub_type>,<buffer>)
- ParameterTypeDescription
sub_type
integerThe binary subtypebuffer
stringThe buffer object containing binary data. Must be a base 64 encoded string value.Returns: A binary data object.
Binary Subtypes
Specify one of the following values for sub_type
:
Number | Description |
---|---|
0 | Generic binary subtype |
1 | Function data |
2 | Binary (old) |
3 | UUID (old) |
4 | UUID |
5 | MD5 |
6 | Encrypted BSON value |
7 | Compressed time series data New in version 5.2. |
8 | Sensitive 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 ### . |
9 | Vector data, which is densely packed arrays of numbers of the
same type. |
128 | Custom data |
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.
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. Similarly, the highest-addressable byte in the BinData
is always interpreted as the most significant byte.
Examples
Insert a BinData()
Object
Use the BinData()
constructor to create the bdata
variable.
var bdata = BinData(0, "gf1UcxdHTJ2HQ/EGQrO7mQ==")
Insert the object into the testbin
collection.
db.testbin.insertOne( { _id : 1, bin_data: bdata } )
Query the testbin
collection for the inserted document.
db.testbin.find()
You can see the binary buffer
stored in the collection.
{ _id: 1, bin_data: Binary(Buffer.from("81fd547317474c9d8743f10642b3bb99", "hex"), 0) }
Get the Length of BinData()
Object
Use the BinData()
constructor to create the bdata
variable.
var bdata = BinData(0, "gf1UcxdHTJ2HQ/EGQrO7mQ==")
Use .length()
to return the bit length of the object.
bdata.length()
The returned value is:
16