Database Reference
In-Depth Information
In our example, the main advantage of the document store is that it can produce
the entire set of data necessary to produce a blog page with a single query. If the blog's
visitor traffic becomes huge, the database records could be split across many machines
without worrying as much about exactly what records ended up on which machine.
The query could simply be routed to a single machine, the entire page content
retrieved at once, and the blog page rendered in a single pass. For very high-volume
Web sites with many more readers than content writers, a document store is likely to
be the best choice for a database backend.
Now imagine that some of the blog's database records featured an author whose
last name was misspelled (this is a use case that I have personally experienced quite a
few times). When using a relational database, author information would be stored in
only one place: a single record in the authors table. Since the data found in a rela-
tional database should be completely normalized, the misspelled value can be easily
changed by issuing a single SQL statement. In contrast, the document database stores
the information about authors in each and every record. If an author produces many
blog posts, that author's name is repeated over and over in every record the author is
associated with. Many of these documents could contain a misspelled author name, so
we would need to iterate over all of the broken records and update them one at a time.
Usually this is done by writing a function that iterates over each individual document
containing the misspelled name field and changing it if found. The normalization
and consistency of the relational database has made this error correction process more
computationally simple and predictable.
Listing 3.3 illustrates inserting and retrieving data from a document storeā€”in this
case, MongoDB. In contrast to a key-value store, it is possible to query for records
based on the data itself rather than having to rely on the key.
Listing 3.3 Inserting and retrieving data using a document store
# Insertion of two documents with different schemas
> db.example.insert({"name":"michael",
"book":"Data Just Right"})
> db.example.insert({"name":"edgar",
"book":"The Relational Model for Database Management",
"employers":["Royal Air Force","IBM"]})
> db.example.find()
{
"_id" : ObjectId("51ab96791a4b9b1009e2c459"),
"name" : "michael",
"book" : "Data Just Right"
}
{
"_id" : ObjectId("51ab9a041a4b9b1009e2c45c"),
"name" : "edgar",
 
Search WWH ::




Custom Search