MongoDB is a lot for many with one request?

In mysql, I use JOIN and one query does not present a problem. how about mongo?

Introduce categories and products.

Products may have more categories. categories may have more product. (from many to many) and the administrator can edit categories in the administration (categories must be separated)

Is it possible to record product with category 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 just get all product categories and product in some category and categories for editing. but if I want to write a product with category names, I need two queries - one to get product category identifiers, and one to get category names from categories using these identifiers.

? ? , , .

+3
2

, MongoDB . . 30 , 3 , , ( ).

MongoDB , .

, , :

categories {
  _id:"mongoid",
  name:"categoryName",
  ...
}

products  {
  _id:"mongoid",
  name:"productName",
  categories:[
    {"4b5783300334000000000bb9":'categoryName'},
    {"5783300334000000000bb943":'CategoryName2'},
    {"6c6793300334001000000116":'categoryName3'}
  ]
}

, ( , ):

db.products.find({"_id": {$in:[1,2,3]}, {name:1,categories:1})

:

db.products.find({"categories.0": {$in:[x,y,z]}},{categories:1,name:1} }

, . . , , .

, , . , , , 100 . 5 , , , .

+4

, , . .

, :

categories {
   name:"categoryName",
         product_id:[ {"4b5783300334000000000aa9": 'productName'},{"5783300334000000000aa943":'productName2'},{"6c6793300334001000000006":'productName3'}]
}


products  {
    name:"productName",
category_id:[{"4b5783300334000000000bb9":'categoryName'},{"5783300334000000000bb943":'CategoryName2',{"6c6793300334001000000116":'categoryName3'}]
}

, .

+1
source

Source: https://habr.com/ru/post/1747095/


All Articles