Database Reference
In-Depth Information
Listing 4.3
An e-commerce order, with line items, pricing, and a shipping address
doc =
{ _id: ObjectId("6a5b1476238d3b4dd5000048")
user_id: ObjectId("4c4b1476238d3b4dd5000001")
state: "CART",
line_items: [
{ _id: ObjectId("4c4b1476238d3b4dd5003981"),
sku: "9092",
name: "Extra Large Wheel Barrow",
quantity: 1,
pricing: {
retail: 5897,
sale: 4897,
}
},
{ _id: ObjectId("4c4b1476238d3b4dd5003981"),
sku: "10027",
name: "Rubberized Work Glove, Black",
quantity: 2,
pricing: {
retail: 1499,
sale: 1299
}
}
],
shipping_address: {
street: "588 5th Street",
city: "Brooklyn",
state: "NY",
zip: 11215
},
sub_total: 6196
}
The second order attribute, user_id , stores a given user's _id . It's effectively a pointer
to the sample user, shown in listing 4.4 (we'll discuss this listing presently). This
arrangement makes it easy to query either side of the relationship. To find all orders
for a given user is simple:
db.orders.find({user_id: user['_id']})
The query for getting the user for a particular order is equally simple:
user_id = order['user_id']
db.users.find({_id: user_id})
Using an object ID as a reference in this way, it's easy to build a one-to-many relation-
ship between orders and users.
We'll now look at some other salient aspects of the order document. In general,
you're using the rich representation afforded by the document data model. You'll see
 
Search WWH ::




Custom Search