Group by in same collection

PHOTO EMBED

Fri Mar 08 2024 10:24:18 GMT+0000 (Coordinated Universal Time)

Saved by @CodeWithSachin ##jupyter #aggregation #mongodb

[
    {
        $group: {
            _id: "$participantId",
            documents: { $push: "$$ROOT" },
            count: { $sum: 1 }
        }
    },
    {
        $match: {
            count: { $gt: 1 }
        }
    },
    {
        $unwind: "$documents"
    },
    {
        $match: {
            "documents.programCode": { $ne: "" }
        }
    },
    {
        $group: {
            _id: {
                participantId: "$_id",
                programCode: "$documents.programCode",
                programStartDate: "$documents.programStartDate"
            },
            documents: { $push: "$documents" },
            count: { $sum: 1 }
        }
    },
    {
        $match: {
            count: { $gt: 1 }
        }
    },
    {
        $project: {
            _id: 0,
            participantId: "$_id.participantId",
            programCode: "$_id.programCode",
            programStartDate: "$_id.programStartDate"
        }
    },
    {
        $lookup: {
            "from": "participant",
            "localField": "participantId",
            "foreignField": "_id",
            "as": "temp"
        }
    },
    {
        $match: {
            "temp.userStatus": { $ne: "TEST" }
        }
    },
    {
        $project: {
            "id":{ $arrayElemAt: ["$temp._id", 0] }
            "email": { $arrayElemAt: ["$temp.email", 0] }
        }
    }
]
content_copyCOPY

http://34.74.16.180:3000/question#eyJkYXRhc2V0X3F1ZXJ5Ijp7ImRhdGFiYXNlIjoyLCJuYXRpdmUiOnsidGVtcGxhdGUtdGFncyI6e30sInF1ZXJ5IjoiW1xyXG4gICAge1xyXG4gICAgICAgIFwiJG1hdGNoXCI6IHtcclxuICAgICAgICAgICAgXCJlbWFpbFwiOiBcInByb2R1Y3RAc21pdC5maXRcIlxyXG4gICAgICAgIH1cclxuICAgIH0sXHJcbiAgICB7XHJcbiAgICAgICAgXCIkbG9va3VwXCI6IHtcclxuICAgICAgICAgICAgXCJmcm9tXCI6IFwidXNlcnNcIixcclxuICAgICAgICAgICAgXCJsb2NhbEZpZWxkXCI6IFwiZW1haWxcIixcclxuICAgICAgICAgICAgXCJmb3JlaWduRmllbGRcIjogXCJlbWFpbFwiLFxyXG4gICAgICAgICAgICBcImFzXCI6IFwidXNlckxrcFwiXHJcbiAgICAgICAgfVxyXG4gICAgfSxcclxuICAgIHtcclxuICAgICAgICBcIiR1bndpbmRcIjoge1xyXG4gICAgICAgICAgICBcInBhdGhcIjogXCIkdXNlckxrcFwiLFxyXG4gICAgICAgICAgICBcInByZXNlcnZlTnVsbEFuZEVtcHR5QXJyYXlzXCI6IHRydWVcclxuICAgICAgICB9XHJcbiAgICB9LFxyXG4gICAge1xyXG4gICAgICAgIFwiJG1hdGNoXCI6IHtcclxuICAgICAgICAgICAgXCJ1c2VyTGtwLnBvcnRhbFBhc3N3b3JkXCI6IFwic21pdEBwcm9kdWN0I0AxMjFcIlxyXG4gICAgICAgIH1cclxuICAgIH0sXHJcbiAgICB7XHJcbiAgICAgICAgXCIkcHJvamVjdFwiOiB7XHJcbiAgICAgICAgICAgIFwiZmlyc3ROYW1lXCI6IDEsXHJcbiAgICAgICAgICAgIFwibWlkZGxlTmFtZVwiOiAxLFxyXG4gICAgICAgICAgICBcImxhc3ROYW1lXCI6IDEsXHJcbiAgICAgICAgICAgIFwiZW1haWxcIjogMSxcclxuICAgICAgICAgICAgXCJtb2JpbGVcIjogMSxcclxuICAgICAgICAgICAgXCJyb2xlXCI6IDEsXHJcbiAgICAgICAgICAgIFwicGFydG5lclNob3J0Q29kZVwiOiAxLFxyXG4gICAgICAgICAgICBcInVzZXJHcm91cHNcIjogXCIkdXNlckxrcC51c2VyR3JvdXBzXCIsXHJcbiAgICAgICAgICAgIFwicHJvZmlsZVBpY3R1cmVVUkxcIjogMSxcclxuICAgICAgICAgICAgXCJwb3J0YWxQYXNzd29yZFwiOiBcIiR1c2VyTGtwLnBvcnRhbFBhc3N3b3JkXCIsXHJcbiAgICAgICAgICAgIFwiYXV0aG9yaXphdGlvblwiOiBcIiR1c2VyTGtwLmF1dGhvcml6YXRpb25cIlxyXG4gICAgICAgIH1cclxuICAgIH1cclxuXSIsImNvbGxlY3Rpb24iOiJ1c2VycyJ9LCJ0eXBlIjoibmF0aXZlIn0sImRpc3BsYXkiOiJ0YWJsZSIsInZpc3VhbGl6YXRpb25fc2V0dGluZ3MiOnt9fQ==