Skip to content

Instantly share code, notes, and snippets.

@timvisher
Created January 7, 2011 22:23
Show Gist options
  • Save timvisher/770217 to your computer and use it in GitHub Desktop.
Save timvisher/770217 to your computer and use it in GitHub Desktop.
import com.gmongo.GMongo
/*
Details example
[weight: 47, weight_units: "lbs", model_num: 1]
*/
/*
Price example
[retail: 589700, sale: 489700]
could also include
[start_date: new GregorianCalendar(2011, 0, 7), end_date: new GregorianCalendar(2011, 1, 7)]
*/
/*
Category example
[slug: "garden", name: "Garden"]
could also include
[parents: [[slug: "home", name: "Home"]], description: "Gardening gadgets galore!"]
*/
/*
Product example
[slug: "wheel-barrow-9092",
sku: "9092",
name: "Extra Large Wheelbarrow",
description: "Heavy duty wheel barrow...",
tags: ["tools", "equipment", "soil"],
price: [retail: 589700, sale: 489700],
price_history: [[retail: 529700, sale: 429700, start_date: new GregorianCalendar(2011, 1, 7), end_date: new GregorianCalendar(2011, 1, 7)]],
category_ids = [ObjectId(...)]]
could also have
[average_reviews: 3.5]
*/
//Demo
//Set up DB Connection
mongo = new GMongo();
mongo.dropDatabase('ecommerce');
db = mongo.getDB('ecommerce');
//Set up Categories
def categories = [[slug: "home", name: "Home"],
[slug: "outdoors", name: "Outdoors"]];
categories << [slug: "gardeningTools",
name: "Gardening Tools",
parents: [[categories[0], categories[1]]],
description: "Gardening gadgets galore!"];
// Commit categories to the DB
db.categories << categories;
// Verify that they're there
assert 3 == db.categories.count;
// Set up products
def products = [[slug: "wheel-barrow-9092",
sku: "9092",
name: "Extra Large Wheelbarrow",
description: "Heavy duty wheel barrow...",
tags: ["tools", "equipment", "soil"],
price: [retail: 589700, sale: 489700],
price_history: [
[retail: 529700,
sale: 429700,
start_date: new GregorianCalendar(2011, 1, 7).getTime(),
end_date: new GregorianCalendar(2011, 1, 7).getTime()],
[retail: 469700,
sale: 369700,
start_date: new GregorianCalendar(2010, 1, 7).getTime(),
end_date: new GregorianCalendar(2010, 1, 7).getTime()]
],
category_ids: [db.categories.findOne(slug: "gardeningTools")._id],
average_reviews: (3.5).doubleValue()]];
// Commit Products
db.products << products;
// Verify save
assert 1 == db.products.count;
assert db.products.findOne().category_ids[0] == db.categories.findOne(slug: "gardeningTools")._id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment