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

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

{“x” : null}

boolean

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

{“x” : true}

number

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

{“x” : 3.14}

or:

{“x” : 3}

integers

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

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

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

string

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

{“x” : “foobar”}

date

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}

array

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.

code

Queries and documents can also contain arbitrary JavaScript code:

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

Excerpt From

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

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

This site uses Akismet to reduce spam. Learn how your comment data is processed.