I am a newbie with mongoDB. I write a simple application to manage product. I use mongoDB. This is my database
Category
Collection:
db.category.insert({id:1,name:"Motor",description:"Sell Motor"})
db.category.insert({id:2,name:"Car":description:"Sell Car"})
Product
Collection:
db.product.insert({id:1,name:"Honda CBR",price:15000,id_category:1})
db.product.insert({id:2,name:"Kawasaki ",price:16000,id_category:1})
db.product.insert({id:3,name:"Ford", price:50000,id_category:2})
I think to find Product
with Category
name is Motor
, I have to write tree queries:
var result = db.category.find({name:"Motor"})
var cat = result.next().id
db.product.find({id_category:cat})
In MySQL I can do the same thing with two table:
CREATE TABLE `product_manage`.`category`(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(2000),
`description` VARCHAR(2000),
PRIMARY KEY (`id`)
);
CREATE TABLE `product_manage`.`product`(
`id` INT NOT NULL,
`name` VARCHAR(2000),
`price` INT,
`id_category` INT,
PRIMARY KEY (`id`)
);
So I don't know what is the advantage of MongoDB over MySQL in this example. I want to ask some more:
In mysql:
case a:
select id into @id_cat where name="Motor"
select * from product where id_category = @id_cat
case b:
select p.id,p.name,p.price from product p,category where p.id_category = category.id
and category.name = "Motor"
Which case is good for performance?