1

Not sure if this can be done, but I gotta ask:

Can I send in multiple aggregation in one request? In other words, rather than doing the following:

one_results = db.results.aggregate([ 
    { $project: { _id: 0, key: "$Field1" }}, 
    { $group: { '_id': '$key', count: { $sum: 1 }}} ])

two_results = db.results.aggregate([ 
    { $project: { _id: 0, key: "$Field2" }}, 
    { $group: { '_id': '$key', count: { $sum: 1 }}} ])

I want to do something like this:

[one_results, two_results] = db.results.aggregate(
  [ 
    { $project: { _id: 0, key: "$Field1" }}, 
    { $group: { '_id': '$key', count: { $sum: 1 }}} 
  ],
  [ 
    { $project: { _id: 0, key: "$Field1" }}, 
    { $group: { '_id': '$key', count: { $sum: 1 }}} 
  ])

I know it's a stretch, but I gotta ask...

Thanks

2
  • I don't think it's do-able. MongoDB doesn't support such a syntax, as yet. But I'll let the experts weigh in. Commented Mar 11, 2014 at 19:44
  • Perhaps you would get a better answer if you showed us the data that you believe you need two queries for. I've already proven some peoples two query theories incorrect: stackoverflow.com/a/22314547/2313887 Commented Mar 12, 2014 at 0:04

2 Answers 2

3

The technical answer is definitely no. It's not supported.

The overhead of making the request to the server pales in comparison to the effort of calculating the aggregation results, so there is little benefit in sending two requests as an array. With many drivers, you could just send two separate requests, even asynchronously giving you similar if not better results (if the load could be distributed).

While you could do multiple calculations in one pipeline, you would want to avoid potentially unrelated aggregation calculations, possibly at greater CPU and IO cost than it's worth, and further, many pipelines wouldn't align well enough to combine them into one.

For example, the first pipeline operator were $match statements that selected a very different subset of documents, it wouldn't be practical to merge them (it's often recommended to try to filter as many documents using an index as the first step of a pipeline).

Sign up to request clarification or add additional context in comments.

1 Comment

Darn. I was hoping there was some magic I could do! Oh well!
2

Yes it's possible using $facet, see mongo documentation

In your use case:

results = db.results.aggregate([ 
{ $facet:
   {
      one_result: [ 
          { $project: { _id: 0, key: "$Field1" }}, 
          { $group: { '_id': '$key', count: { $sum: 1 }}} ],
      two_result: [ 
          { $project: { _id: 0, key: "$Field2" }}, 
          { $group: { '_id': '$key', count: { $sum: 1 }}}  ],
   }
}];

// result 1
... = results[0].one_result

// result 2
... = results[0].two_result 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.