Databases Reference
In-Depth Information
> doc.myInteger.floatApprox
3
All 4-byte integers can be represented exactly by an 8-byte floating-point number, so
they are displayed normally.
Dates
In JavaScript, the Date object is used for MongoDB's date type. When creating a new
Date object, always call new Date(...) , not just Date(...) . Calling the constructor as a
function (that is, not including new ) returns a string representation of the date, not an
actual Date object. This is not MongoDB's choice; it is how JavaScript works. If you
are not careful to always use the Date constructor, you can end up with a mishmash of
strings and dates. Strings do not match dates, and vice versa, so this can cause problems
with removing, updating, querying…pretty much everything.
For a full explanation of JavaScript's Date class and acceptable formats for the con-
structor, see ECMAScript specification section 15.9 (available for download at http://
www.ecmascript.org ) .
Dates in the shell are displayed using local time zone settings. However, dates in the
database are just stored as milliseconds since the epoch, so they have no time zone
information associated with them. (Time zone information could, of course, be stored
as the value for another key.)
Arrays
Arrays are values that can be interchangeably used for both ordered operations (as
though they were lists, stacks, or queues) and unordered operations (as though they
were sets).
In the following document, the key "things" has an array value:
{"things" : ["pie", 3.14]}
As we can see from the example, arrays can contain different data types as values (in
this case, a string and a floating-point number). In fact, array values can be any of the
supported values for normal key/value pairs, even nested arrays.
One of the great things about arrays in documents is that MongoDB “understands”
their structure and knows how to “reach inside” of arrays to perform operations on
their contents. This allows us to query on arrays and build indexes using their contents.
For instance, in the previous example, MongoDB can query for all documents where
3.14 is an element of the "things" array. If this is a common query, you can even create
an index on the "things" key to improve the query's speed.
MongoDB also allows atomic updates that modify the contents of arrays, such as
reaching into the array and changing the value pie to pi . We'll see more examples of
these types of operations throughout the text.
 
Search WWH ::




Custom Search