Docs Menu
Docs Home
/ /

HexData() (mongosh method)

Creates a binary data object from hexadecimal data.

HexData() has the following syntax:

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

The command takes these fields:

Field
Type
Necessity
Description

subType

integer

Required

Specify a data subtype:

Subtype
Description

0

Generic binary subtype

1

Function

2

Byte Array

3

OLD UUID

4

UUID

5

MD5

128

User defined

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.

Note

mongosh silently stops parsing at the first non-hex character, unlike the legacy shell which rejects invalid hex characters. To determine if your HexData() output is valid, you can use the .toString('hex') method on the HexData() output and compare that string against the original input string using a case-insensitive comparison.

1

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

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) } ]

Back

Date

On this page