How to ignore a mongo pipeline stage conditionally based on field values?

addFields { "items.product.title": "some title", "items.product.code": "some code" } This is an addFields stage in my query pipeline. Sometimes items field is an empty array before the execution of this stage and some other times it has items which also need product field to be added (in this stage). I want to make a condition … Read more

MongoDB aggregate Lookup remove array

I am currently using an aggregate lookup method from mongoose and it works: const data = await this.messageModel.aggregate([ { $match: { channel_id: channel_id }, }, { $limit: limit ? limit : 1000, }, ]).lookup({ from: 'users', localField: 'author', foreignField: 'id', as: 'author', pipeline: [ { $project: { _id: 0, password: 0, email: 0, __v: 0 … Read more