Q
Q
qovalenko2019-05-30 08:47:56
MongoDB
qovalenko, 2019-05-30 08:47:56

How to properly optimize a query in MongoDB?

Documents have the following structure:

{
  "oneCategory" : "...",
  "twoCategory" : "...",
  "threeCategory" : "...",
  "fourCategory" : "...",
  "fiveCategory" : "...",
  "count" : 5
}

Right now I have several requests:
  1. Grouping to get the sum of count oneCategory is (Plates: 500, Cups: 100)
  2. Get number of twoCategory where oneCategory = Plates is (Blue: 200, Red: 60, Yellow: 240)
  3. Get the quantity of threeCategory where oneCategory = Plates and twoCategory = Yellow is (With pattern: 150, without pattern: 90)
  4. Get quantity fourCategory where oneCategory = Plates and twoCategory = Yellow, threeCategory = Patterned is (Square: 50, Round: 100)
  5. Get the number of fiveCategory where oneCategory = Cymbals and twoCategory = Yellow, threeCategory = Patterned, fourCategory = Round is (Glass: 50, Clay: 50)

Now how can I compose this in one query. Thanks!!!
The goal is to get all the values ​​obtained during processing, not just the result in step 5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
forspamonly2, 2019-05-30
@qovalenko

monga now has facet search built into the aggregation framework: $facet

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question