Database Manual / Reference / mongosh Methods / Object Constructors

HexData() (mongosh method)

Definition

Creates a binary data object from hexadecimal data.

Syntax

HexData() has the following syntax:

HexData( <subType>, <buffer> )
Returns:Binary data object.

Command Fields

The command takes these fields:

FieldTypeNecessityDescription

subType

integer

Required

Specify a data subtype:

NumberDescription

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

buffer

string

Required

Hexadecimal data. The string is decoded up to the first character that is not a valid hexadecimal digit. You can use upper or lower case letters in the hexadecimal string.

Examples

1

Create the example collection

Use HexData() to insert a document with binary data created from a hexadecimal string into a test collection:

db.hexCollection.insertOne( {
_id: 0, hexField: HexData( 0, "123456abcdef" )
} )
2

Retrieve the document

To return the document, run the following find command:

db.hexCollection.find()

Output shows the hexField value as a base 64 number using Binary.createFromBase64():

[ { _id: 0, hexField: Binary.createFromBase64('EjRWq83v', 0) } ]

Learn More