For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

$subtype (expression operator)

New in version 8.3. :

$subtype

Returns the subtype of a given value as an integer.

$subtype has the following syntax:

{ $subtype: <expression> }

The <expression> can be any valid expression that contains a subtype.

Note

In MongoDB 8.3, the only expression that contains a subtype is a BinData expression.

$subtype operations on null or missing values return null.

$subtype operations on expressions that do not have a subtype return an error.

$subtype operations on BinData expressions return the binary subtype of the expression. For more information, see Binary Subtypes.

Use the BinData() constructor to create a bdata variable.

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

The following operation outputs the subtype of the bdata object:

{ $subtype: bdata }

The expression returns 0.