0

I have a collection that looks like this:

{'start': '2011-01-29',
  'target': [0.0, 2.0],
  'cat': [0, 0, 0, 0, 0],
  'state': 0},
...

I had to do some aggregations in this collection using db.collections.aggregate([]), and because of performance issues I had to separate into two queries.

The first one returns me:

{'_id': {'state': 0, 'id': 0},
  'start': '2011-01-29',
  'cat': [0, 0, 0, 0, 0]}
...

The second one returns me:

{'_id': {'state': 0, 'id': 0},
  'target': [0, 2, 1]
  }
...

What I would like to know, if there is a way to join again the results based on _id?

Or is there a way to declare both query independently into the same one ?

db.collections.aggregate([
query1: {},
query2: {}
concat(query1,query2) ])

My desired output:

Combine again both outputs

{'_id': {'state': 0, 'id': 0},
  'start': '2011-01-29',
  'cat': [0, 0, 0, 0, 0]}
...

{'_id': {'state': 0, 'id': 0},
  'target': [0, 2, 1]
  }
...

Into this

{'_id': {'state': 0, 'id': 0},
  'start': '2011-01-29',
  'target': [0, 2, 1]
  'cat': [0, 0, 0, 0, 0]}
...

My solution so far was using pymongo:

x1 = db.collection.aggregate(query1)

x2 = db.collection.aggregate(query2)

for i, j in zip(x1, x2):
    j['target'] = i['target']
3
  • I would suggest reviewing available aggregation pipeline operators and/or revising your question to say what you ultimately want to achieve. Commented Apr 28, 2020 at 19:45
  • Ok, I have just updated. Commented Apr 28, 2020 at 20:15
  • You are posing an XY problem. State what the X is. Commented Apr 28, 2020 at 20:22

2 Answers 2

1

You can get that behavior with a $facet stage, and then use $group to bring them together

db.collection.aggregate([
   {$facet:{
       query1:[pipeline1],
       query2:[pipeline2]
   }},
   {$project:{
       result:{$concatArrays:["$query1","$query2"]}
   }},
   {$unwind: "$result"},
   {$group: {
         _id:"$_id",
         document: {$mergeObjects:"$$ROOT"}
   }},
   {$replaceRoot:{ newRoot: "$document"}}
])
Sign up to request clarification or add additional context in comments.

Comments

1

To answer your question as stated, it is not directly possible to send your individual result sets to MongoDB to have it perform more operations on them. It doesn't make sense to do so because to construct such a request would require having both result sets in memory, at which point you should simply merge them in the application instead of performing this same operation in the database AND moving the data back and forth over the network.

Technically you could create two temporary collections, insert each result set into its respective collection, then perform another aggregation...

1 Comment

Thank you @Oleg. So, I will use pymongo.

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.