I want to join/group differents collections in one

My array subscribers is full of Object like this for example:

_id:5fc521659d588f6ab03661ff
uid:"FX8BBAH3VBH8T8051D"
reference:"FX8BBAH3VBH8T8051D"
total:0
setupFee:0
test:false
customerData:Object
sources:Array
subscription:5bf36db84bcbac7920daa39f
status:"active"
customer:5b8dd7bf26d681000f90bc20
startDate:2020-11-30T16:44:21.867+00:00
agenda:Array
customerToken:Array
executions:Array
created:2020-11-30T16:44:21.870+00:00
updated:2020-11-30T16:44:21.875+00:00
__v:0

(I took that directly from Compass)

That’s my stage right before $unwind.

1 Like

NOOOOO, you are the best, It was my fault…I didn’t put two $$ in my Compass, you made my day, sice friday I want to do this…

1 Like

1 Like

Hi, I think that the result is not what I need, can I make a new topic of this problem but with more details and examples?

1 Like

Of course! Ping me there with @MaBeuLux88.

1 Like

Before to star that, I want to show you this when I put .toArray() at the end of the aggregate on MONGOSH:

[
  {
    subs: [
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object]
    ]
  }
]

why the return is like a matrix of 4x12 dimension?

It’s just display. Subs is just an array of 48 elements here. It’s just line wrapping to avoid going out of the screen.

Can I show 'subs" in js o ts as a return of a method?

Yes of course. Why would it be impossible?

how can i do that? Because I don’t know how to do that because I have this:

class subscriberQuery{

  get subscriptions() {
    const MobbexDB = Mobbex.instance.storage.db;
    return MobbexDB.getModel("appsubscriptionsplans");
  }

  get subscribers() {
    const MobbexDB = Mobbex.instance.storage.db;
    return MobbexDB.getModel("appsubscriptionssubscribers");
  }

  public async getData(filters: any = {}, params: any = {}, batch: QueryBatchParams = { size: 500, skip: 0 }) {
    return this.getSubscribers(filters, params, batch);
  }

  public async getSubscribers(filters : any = {}, params : any = {}, batch: QueryBatchParams = {size : 500, skip : 0}) {
    return this.subscriptions
        .aggregate([
            {
              $match: {
                entity: params.entity, 
                status: 'active'
              }
            }, 
            {
              $lookup: {
                from: 'appsubscriptionssubscribers', 
                localField: '_id', 
                foreignField: 'subscription', 
                as: 'subscribers'
              }
            }, 
            {
              $match: {
                'subscribers.0': {
                  $exists: true
                }
              }
            },
            { $skip : batch.skip },
            { $limit : batch.size },  
            {
              $unwind: {
                path: '$subscribers'
              }
            }, 
            {
              $group: {
                _id: null, 
                subs: {
                  $push: '$$ROOT'
                }
              }
            },
            {
              $project: {
                _id: 0, 
                subs: 1
              }
            }
          ])
          .toArray();
  }
}

and I have to return in getData ‘subs’ and I don’t know how to extract that field…

Checkout this blog post:

I think toArray() returns a Promise. Then you can just extract the value of subs from the doc once the Promise is resolved.

Not a JS expert here. :confused:

1 Like

how can I extract subs?..I am lost right now again, I created a variable call doc so I can save there await this.getSubscribers(filters, params, batch); but when I show that the result is this: [], any suggestion?

const {MongoClient} = require('mongodb');

async function aggregation(client, pipeline) {
  return client.db("test").collection("coll").aggregate(pipeline).toArray();
}

async function main() {

  const uri = "mongodb://localhost/test";
  const client = new MongoClient(uri);
  const pipeline = [{
    "$match": {name: "Max"}
  }, {
    "$project": {_id: 0, my_array: 1}
  }];

  try {
    await client.connect();
    const coll = client.db("test").collection("coll");
    await coll.drop();
    const res = await coll.insertOne({name: "Max", my_array: [1, 2, 3]});
    console.log("Insert OK:", res);
    const docs = await aggregation(client, pipeline);
    const doc = docs[0];
    console.log("Entire doc:", doc);
    console.log("Just the array:", doc.my_array);
  } finally {
    await client.close();
  }
}

main().catch(console.error);

Result:

Insert OK: {
  acknowledged: true,
  insertedId: new ObjectId("6297c95481a3164ee6034d5b")
}
Entire doc: { my_array: [ 1, 2, 3 ] }
Just the array: [ 1, 2, 3 ]
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.