Database Reference
In-Depth Information
pricing: {
retail: 589700,
sale: 489700,
},
price_history: [
{retail: 529700,
sale: 429700,
start: new Date(2010, 4, 1),
end: new Date(2010, 4, 8)
},
{retail: 529700,
sale: 529700,
start: new Date(2010, 4, 9),
end: new Date(2010, 4, 16)
},
],
category_ids: [new ObjectId("6a5b1476238d3b4dd5000048"),
new ObjectId("6a5b1476238d3b4dd5000049")],
main_cat_id: new ObjectId("6a5b1476238d3b4dd5000048"),
tags: ["tools", "gardening", "soil"],
}
The document contains the basic name , sku , and description fields. There's also the
standard MongoDB object ID stored in the _id field. In addition, you've defined a
slug, wheel-barrow-9092 , to provide a meaningful URL . MongoDB users sometimes
complain about the ugliness of object ID s in URL s. Naturally, you don't want URL s that
look like this:
http://mygardensite.org/products/4c4b1476238d3b4dd5003981
Meaningful ID s are so much better:
http://mygardensite.org/products/wheel-barrow-9092
I generally recommend building a slug field if a URL will be generated for the docu-
ment. Such a field should have a unique index on it so that the value can be used as a
primary key. Assuming you're storing this document in the products collection, you
can create the unique index like so:
db.products.ensureIndex({slug: 1}, {unique: true})
If you have a unique index on slug , you'll need to insert your product document
using safe mode so that you'll know if the insert fails. That way, you can retry with a
different slug if necessary. To take an example, imagine your gardening store has mul-
tiple wheelbarrows for sale. When you start selling a new wheelbarrow, your code will
need to generate a unique slug for the new product. Here's how you'd perform the
insert from Ruby:
@products.insert({:name => "Extra Large Wheel Barrow",
:sku => "9092",
Search WWH ::




Custom Search