1

I have two collections as follows

const DashboardSchema = new Schema({
    userId: {
        type: SchemaTypes.ObjectId
    }
}, { toJSON: { virtuals: true }, toObject: { virtuals: true } });

const reportSchema = new Schema({
  userId: Schema.Types.ObjectId,
  seq: Number,
  date: Date
});

we will have multiple reports for single user. I want to get all user reports based on userId

I have tried like this

return await Dashboard.aggregate([

          {
            "$match": {
                "userId": "5bb0b0f408458a675419d575"//sample id
            }
        }
        {
            "$lookup": {
                "from": reportSchema.collection.name,
                "let": { "userId": "$userId" },
                "pipeline": [
                    {
                        "$match": {
                            "$expr":
                            {
                                "$eq":
                                    ["$userId", "$$userId"]
                            }
                        }
                    },
                    { "$sort": { "date": -1 } },
                    { "$limit": 10 }
                ],
                "as": "Reports"
            }
        },

    ]);

but it is return null. Any thoughts?

krishna
  • 151
  • 4
  • 15

0 Answers0