May 05, 2010

Posted by John

« older newer »

MongoDB and E-commerce

If you ask me, Kyle Banker does not write about MongoDB enough. Yep, I am going to call him out. I say this because every time he does I learn something new (or at least think about something in a different way).

Previously, he wrote about aggregation (count, group, and map/reduce) and how flexible the Ruby driver is. This time, he has posted on MongoDB and E-commerce. The post goes into schema examples for catalog management, shopping carts, and orders. He also touches on building reports using map/reduce and updating orders/inventory.

Positional Operator

I have mentioned it before, but I am excited about the $ positional operator. In the post, Kyle shows how to use it to update an order line item in place:

db.orders.update(
  {'_id': order_id, 'line_items.sku':'jc-431'}, 
  {'$set': {'line_items.$.quantity': 2}}
);

He also shows how to atomically add line items and increment the order subtotal using $push and $inc:

db.orders.update(
  {'_id': order_id},
  {
    '$push': {'line_items': {'sku': 'md-12', 'price': 2500, 'title': 'Basketball'}}, 
    '$inc': {'subtotal': 2500}
  }
);

$push used in this manner is quite handy. We do something similar in Harmony when you approve a spam comment, performing a $push to add the comment to the blog post document.

At any rate, the article is a great read. Go check it out.

Labels: Elsewhere

2 Comments

  1. Have you given any though to how such techniques might make it into an ORM layer?

  2. Rodrigo Rodrigo

    Sep 15, 2010

    @John I think that mongoid alread does that.

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a web developer and programmer who has fallen deeply in love with Mongo. More about John.

Syndication

Feed IconMongoTips Articles - An assortment of news, howto's and thoughts on MongoDB.