Databases Reference
In-Depth Information
“item” : {
“name” : “regular”,
“unit_price” : 2
},
“quantity” : 2
}
]
}
> db.orders.save(t);
> db.orders.find();
{ “_id” : ObjectId(“4cccff35d3c7ab3d1941b103”), “order_date” : “Sat Oct 30 2010
22:30:12 GMT-0700 (PDT)”, “line_items” : [
...
] }
coffee_order.txt
Although storing the entire nested document collection is advised, sometimes it's necessary to store
the nested objects separately. When nested documents are stored separately, it's your responsibility
to join the record sets together. There is no notion of a database join in MongoDB so you must
either manually implement the join operation by using the object id on the client side or leverage the
concept of DBRef .
In MongoDB DBRef is a formal specifi cation for creating references between
documents. A DBRef includes a collection name as well as an object id. Read
more about MongoDB DBRef at www.mongodb.org/display/DOCS/Database+
References#DatabaseReferences-DBRef .
You can restructure this example in a way that doesn't store the unit price data for a product in
the nested document but keeps it separately in another collection, which stores information on
products. In the new format, the item name serves as the key to link between the two collections.
Therefore, the restructured orders data is stored in a collection called orders2 as follows:
> t2 = {
... order_date: new Date(),
... “line_items”: [
... {
... “item_name”:”latte”,
... “quantity”:1
... },
... {
... “item_name”:”cappuccino”,
... “quantity”:1
... },
... {
... “item_name”:”regular”,
Available for
download on
Wrox.com
Search WWH ::




Custom Search