MongoDB Supported Data Types

November 23, 2013
MongoDB Supported Data Types

MongoDB adds support for a number of additional data types while keeping JSON’s essential key/value pair nature. Exactly how values of each type are represented varies by language, but this is a list of the commonly supported types and how they are represented as part of a document in the shell. The most common types are:


Null can be used to represent both a null value and a nonexistent field:

{“x” : null}


There is a boolean type, which can be used for the values true and false:

{“x” : true}


The shell defaults to using 64-bit floating point numbers. Thus, these numbers look “normal” in the shell:

{“x” : 3.14}


{“x” : 3}


For integers, use the NumberInt or NumberLong classes, which represent 4-byte or 8-byte signed integers, respectively.

{“x” : NumberInt(“3”)}

{“x” : NumberLong(“3”)}


Any string of UTF-8 characters can be represented using the string type:

{“x” : “foobar”}


Dates are stored as milliseconds since the epoch. The time zone is not stored:

{“x” : new Date()}

regular expression

Queries can use regular expressions using JavaScript’s regular expression syntax:”

“{“x” : /foobar/i}


Sets or lists of values can be represented as arrays:

{“x” : [“a”, “b”, “c”]}

embedded document

Documents can contain entire documents embedded as values in a parent document:

{“x” : {“foo” : “bar”}}

object id

An object id is a 12-byte ID for documents. See the section _id and ObjectIds for details:

{“x” : ObjectId()}

There are also a few less common types that you may need, including:

binary data

Binary data is a string of arbitrary bytes. It cannot be manipulated from the shell. Binary data is the only way to save non-UTF-8 strings to the database.


Queries and documents can also contain arbitrary JavaScript code:

{“x” : function() { /* … */ }}

Excerpt From

Kristina Chodorow. “MongoDB_ The Definitive Guide, 2nd Edition.epub.” iBooks.