Database Reference
In-Depth Information
representation. It's also partly due to drivers, which provide a fairly high-level inter-
face to MongoDB. You can frequently build entire applications on MongoDB using
the driver interface alone.
That said, object mappers are convenient because they facilitate validation, type
checking, and associations. A number of mature MongoDB object mappers provide an
extra layer of abstraction above the basic language drivers, and you might consider
using one on a larger project. 1 But regardless of the object mapper, you're always ulti-
mately dealing with documents. That's why this chapter focuses on the documents
themselves. Knowing the shape of documents in a well-designed MongoDB schema will
prepare you to work with the database intelligently, with or without an object mapper.
4.2.1
Products and categories
Products and categories are the mainstays of any e-commerce site. Products, in a nor-
malized RDBMS model, tend to require a large number of tables. There's always a
table for basic product information, such as the name and SKU, but there'll be other
tables to relate shipping information and pricing histories. If the system allows prod-
ucts with arbitrary attributes, then a complicated series of tables will be necessary to
define and store those attributes, as you saw in chapter 1 in the Magento example.
This multitable schema will be facilitated by the RDBMS 's ability to join tables.
Modeling a product in MongoDB should be less complicated. Because collections
don't enforce a schema, any product document will have room for whichever
dynamic attributes the product needs. And by using arrays to contain inner docu-
ment structures, you can typically condense a multitable RDBMS representation into a
single MongoDB collection. More concretely, here's a sample product from a garden-
ing store.
Listing 4.1
A sample product document
doc =
{ _id: new ObjectId("4c4b1476238d3b4dd5003981"),
slug: "wheel-barrow-9092",
sku: "9092",
name: "Extra Large Wheel Barrow",
description: "Heavy duty wheel barrow...",
details: {
weight: 47,
weight_units: "lbs",
model_num: 4039283402,
manufacturer: "Acme",
color: "Green"
},
total_reviews: 4,
average_review: 4.5,
1
To find out which object mappers are most current for your language of choice, consult the recommenda-
tions at http://mongodb.org .
Search WWH ::




Custom Search