0

I have multiple queries which all have a shared id.

I am trying to join these queries into a single output but can not seem to figure it out.

Any ideas? Thank you!


Collection contains customers and sellers (id of each, date of sale, etc.). The intended output is one that contains (seller id, buyer id, number of time seller has sold an item, number of distinct customer seller has sold to, array of distinct customers seller has sold to, array of transaction id).

I've been able to query each component, for example:

Array of seller ids seller has sold to

db.completedSessions.aggregate([
{
  $group:
  {
    _id: {seller: "$sellerId"}, customer_id_array: {$push: "$customerId"}
   }
} 
])

Count of sales by seller

db.completedSessions.aggregate([
{
  $group:
  {
    _id: {user: "$sellerId"}, count: {$sum: 1}
  }
} 
])

I'm trying to join these queries together using the sellerId.

  • 1
    Can you post samples of your queries and output? – Megidd Apr 19 '18 at 17:23
  • @user3405291 Edited! – user6132211 Apr 19 '18 at 19:00
  • Answer is the same, you use `$lookup`. MongoDB 3.4 introduced ["views"](https://docs.mongodb.com/manual/core/views/), which are essentially just aggregation pipelines that "appear to be collections". So create a view using at least one of the pipelines you have and then use it's "collection" name as the source for `$lookup` – Neil Lunn Apr 20 '18 at 05:41
  • @NeilLunn Thank you for the response! Could you clarify how to use "views" in this case? – user6132211 Apr 22 '18 at 19:51

0 Answers0