Databases Reference
In-Depth Information
Basic Data Types
Documents in MongoDB can be thought of as “JSON-like” in that they are conceptually
similar to objects in JavaScript. JSON is a simple representation of data: the specifica-
tion can be described in about one paragraph ( http://www.json.org proves it) and lists
only six data types. This is a good thing in many ways: it's easy to understand, parse,
and remember. On the other hand, JSON's expressive capabilities are limited, because
the only types are null, boolean, numeric, string, array, and object.
Although these types allow for an impressive amount of expressivity, there are a couple
of additional types that are crucial for most applications, especially when working with
a database. For example, JSON has no date type, which makes working with dates even
more annoying than it usually is. There is a number type, but only one—there is no
way to differentiate floats and integers, never mind any distinction between 32-bit and
64-bit numbers. There is no way to represent other commonly used types, either, such
as regular expressions or functions.
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 rep-
resented as part of a document in the shell:
null
Null can be used to represent both a null value and a nonexistent field:
{"x" : null}
boolean
There is a boolean type, which will be used for the values 'true' and 'false' :
{"x" : true}
32-bit integer
This cannot be represented on the shell. As mentioned earlier, JavaScript supports
only 64-bit floating point numbers, so 32-bit integers will be converted into those.
64-bit integer
Again, the shell cannot represent these. The shell will display them using a special
embedded document; see the section “Numbers” on page 18 for details.
64-bit floating point number
All numbers in the shell will be of this type. Thus, this will be a floating-point
number:
{"x" : 3.14}
As will this:
{"x" : 3}
string
Any string of UTF-8 characters can be represented using the string type:
 
Search WWH ::




Custom Search