mongoDB many to many with one query?
Posted
by PowderKeg
on Stack Overflow
See other posts from Stack Overflow
or by PowderKeg
Published on 2010-05-25T21:41:44Z
Indexed on
2010/05/26
2:01 UTC
Read the original article
Hit count: 235
mongodb
in mysql i use JOIN and one query is no problem. what about mongo?
imagine categories and products.
products may have more categories. categories may have more product. (many to many structure) and administrator may edit categories in administration (categories must be separated)
its possible write product with categories names in one query?
i used this structure
categories {
name:"categoryName",
product_id:["4b5783300334000000000aa9","5783300334000000000aa943","6c6793300334001000000006"]
}
products {
name:"productName",
category_id:["4b5783300334000000000bb9","5783300334000000000bb943","6c6793300334001000000116"]
}
now i can simply get all product categories, and product in some category and categories alone for editation. but if i want write product with categories names i need two queries - one to get product categories id and second to get categories names from categories by that ids.
is this the right way? or this structure is unsuitable? i would like to have only one query but i dont know if its possible.
© Stack Overflow or respective owner