Skip to main content
Version: v3.x (DDN)

Connector Types

Although MongoDB is schema-less it does use a set of types for storing data. Specifically MongoDB stores data in BSON format. BSON is similar to JSON except that it is a binary instead of a text format, and BSON includes a number of types that are not distinguished in JSON. For example BSON includes several numeric types while JSON only has one type for numbers.

Choices of types determine what operations are available for filtering and sorting data. Types also determine how data from your database will be represented in JSON since all data coming into and out of your database through the GraphQL API is converted from BSON to JSON or vice versa.

This page describes the types that MongoDB supports, and provides information about type-related configuration in the MongoDB connector.

Scalar types

The MongoDB connector supports all of the scalar types defined by BSON (except dbPointer), plus a first-class UUID type (technically in BSON UUIDs are stored using the binData scalar type), and a special type called extendedJSON which represents all possible BSON values. These are the types available:

TypeDetails
extendedJSONthe connector's "any" type - this type can hold any possible value
int32-bit integer
long64-bit integer
double64-bit float
decimal128-bit float
stringUTF-8 encoded string
datedate with time
timestampused internally by MongoDB - use date instead
binDatabinary data with a "subtype" that indicates how the data is interpreted
uuidUUIDs such as those generated by the mongosh new UUID() constructor (added in connector v1.7)
objectIdthe default document identifier used by MongoDB
bool
null
regexregular expression
javascriptJavaScript code
javascriptWithScopeJavaScript code with bindings for closure variables
minKeydefined to be less than any other value for comparisons, regardless of type
maxKeydefined to be greater than any other value for comparisons, regardless of type
undefineddeprecated by MongoDB
dbPointerdeprecated by MongoDB, not supported by connector
symboldeprecated by MongoDB
Scalar type names in GraphQL

The table above gives names for types as they are used in connector configuration which is taken from names in BSON documentation. On the other hand, GraphQL uses uppercase type names. In your GraphQL schema and in metadata configuration in .hml files you will see these type names with the first letter uppercased.

Complex types

The complex types that the connector recognizes are:

Type ConstructorDetails
arrayhomogenous array
objectrecord type with a fixed set of string keys, and a distinct type for each key
nullablevalues of a nullable type might be null

For details on how to write complex types see Type Expressions.

Representations in JSON

GraphQL operates using JSON; to interact with BSON data in your database, the connector must convert inputs (such as filter arguments or mutation inputs) from JSON to BSON, and must convert result data from BSON to JSON. These conversions are determined by the types configured in your connector's schema.

Extended JSON

The extendedJSON type represents all possible BSON values. Fields of this type are converted to JSON differently from fields with more specific types. See Extended JSON.

In general, conversions between JSON and BSON are symmetric: input values will look the same as response data. Here are JSON conversion details for each scalar type:

TypeExample JSONDetails
extendedJSON{ "$numberInt": "3" }see Extended JSON
int3JSON number
long"3"converted to a string to avoid accidental loss of precision
double3.14JSON number
decimal"3.14"converted to a string to avoid accidental loss of precision
string"Hello, world!"JSON string
date"2016-03-04T00:00:00.000000000Z"string in ISO-8601 format
timestamp{ "t":1565545664, "i":1 }use date instead
binData{ "base64": "EEEBEIEIERA=", subType: "00" }base64-encoded data and interpretation hint - see BSON docs
uuid"40a693d0-c00a-425d-af5c-535e37fdfe9c"string in UUID format
objectId"66135e86eed2c00176f6fbe6"string with hex encoding - the same format that ObjectId stringifies to in mongosh
booltrue
nullnull
regex{ "pattern": "^H", "options": "i" }JSON object with separate fields for pattern and options - inputs may be given as a plain string for a pattern without options
javascript"function(x) { return x + 1 }"JSON string
javascriptWithScope{ "$code": "function(x) { return x + y }", "$scope": { "y": "1" } }JSON object with code as string, and mappings for captured variables
minKey{}empty object
maxKey{}empty object
undefinednullJSON doesn't have an undefined so BSON undefined converts to null instead
symbol"foo"JSON string
dbPointernot supported by connector