tags:

views:

86

answers:

2

Hi!

I have very interested in using MongoDB it seems awesome. But I'm from a totally different school : relational databases.

So now I'm wondering how would this case works with MongoDB:

Say, I have a table filled with brands and I have another table filled with products.

Each products will have a brand. This is very simple to understand but I still don't get how would this works with MongoDB?

I mean, would I have to repeat the brand each time I add a product? Can I do some kind of relations?

Thanks for enlightening me :)

+1  A: 

One way would be to set it up similar to this:

{'brand':'brand one', 'products':
                       [{'product name':'a fine product','price':'$50'},
                        {'product name':'yet another fine product','price':'$20'}]
},
{'brand':'brand two', 'products':
                       [{'product name':'brand two product','price':'$10'}]
}

In this case you only have one 'table' with all the information you need on the products (including brand). I have only done some experimenting with mongodb so I'm not sure how this would scale.

It is a different way of thinking from a relational DB and a nosql solution shouldn't be used in all cases.

sriehl
Oh! okay so what you're telling me is my ""relations"" should be in the way I "align" my models? From the highest to the lowest so it would make something like this:Blog->Author->Article->Comments.It's pretty logic! So I always have everything I need to use... Thanks for the help :)
Tom
+1  A: 
  1. Insert brands
  2. Insert insert products (with brands)
  3. Query it

Preparation:

  • Download http://www.mongodb.org/downloads
  • On Windows create dir c:\data\db\ (the default dir, don't care much about it now)
  • Run mongod (it will run a server)
  • Run mongo (it will run a client and use default test database)

Brands:

db.things.save({'name': 'Ford'});
db.things.save({'name': 'Mitsubishi'});

Products:

db.things.save({'brand': 'Ford', 'name': 'Mustang'});
db.things.save({'brand': 'Ford', 'name': 'Falcon'});
db.things.save({'brand': 'Mitsubishi', 'name': 'Delica'});
db.things.save({'brand': 'Mitsubishi', 'name': 'L300'});

Querying:

db.things.find();
db.things.find({'brand': 'Ford'});
db.things.find({'brand': 'Mitsubishi'});

// I just learned this (incl. downloading etc) almost before the first answer was posted from tutorial and manual in general. Nice experience :)

dwich
Thanks for the answer. But what if, I need to rename one brand? Does it create many entry of the brand throughout the database or it only references it?
Tom
Rename: `db.things.update({"name": "L300" }, {"brand": "Mitsubishi", "name": "L300 4WD"});` I suppose it keeps just one entry for one object and references it. Every single entry contains ObjectId "_id" which seems same all the time for me so I suppose it updates this entry instead of removing and inserting it.
dwich
dwich
Thanks for the help! On http://www.mongodb.org/ you can actually try everything on their javascript console! It's awesome and now I get it! Thanks!!!
Tom