Database Reference
In-Depth Information
Using Documents
Recall that a
document
consists of key-value pairs. For example, the pair
"type" : "Book"
consists of a key named
type
, and its value,
Book
. Keys are written as
strings
, but the
values in them can vary tremendously. Values can be any of a rich set of datatypes,
such as arrays or even binary data. Remember: MongoDB stores its data in BSON format
(see Chapter 1 for more information on this topic).
Next, let's look at all of the possible types of data you can add to a document, and
what you use them for:
•
String
: This commonly used datatype contains a string of text
(or any other kind of characters). This datatype is used mostly for
storing text values (for example,
"Country" : "Japan"
}.
•
Integer
(32b and 64b)
: This type is used to store a numerical value
(for example,
{ "Rank" : 1 }
). Note that there are no quotes
placed before or after the integer.
•
Boolean
: This datatype can be set to either
TRUE
or
FALSE
.
•
Double
: This datatype is used to store floating-point values.
•
Min / Max
keys
: This datatype is used to compare a value against
the lowest and highest BSON elements, respectively.
•
Arrays
: This datatype is used to store arrays (for example,
[
"Membrey, Peter","Plugge, Eelco","Hows, David"]
).
•
Timestamp
: This datatype is used to store a timestamp. This can
be handy for recording when a document has been modified or
added.
•
Object
: This datatype is used for embedded documents.
•
Null
: This datatype is used for a
Null
value.
•
Symbol
: This datatype is used identically to a string; however, it's
generally reserved for languages that use a specific symbol type.
•
Date
*
: This datatype is used to store the current date or time in
Unix time format (POSIX time).
•
Object
ID
*
: This datatype is used to store the document's ID.
•
Binary
data
*
: This datatype is used to store binary data.
•
Regular
expression
*
: This datatype is used for regular expressions.
All options are represented by specific characters provided in
alphabetical order. You will learn more about regular expressions
in Chapter 4.
•
JavaScript
Code
*
: This datatype is used for JavaScript code.