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.
The asterisks mean that the last five datatypes (date, object ID, binary data, regex, and JavaScript code) are
non-JSON types; specifically, they are special datatypes that BSON allows you to use. In Chapter 4, you will learn how
to identify your datatypes by using the $type operator.
In theory, this all probably sounds straightforward. However, you might wonder how you go about actually
designing the document, including what information to put in it. Because a document can contain any type of data,
you might think there is no need to reference information from inside another document. In the next section, we'll
look at the pros and cons of embedding information in a document compared to referencing that information from
another document.
 
Search WWH ::




Custom Search