MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

My aggregate query not working

I am getting only _id in output for following query await User.aggregate([
    {
        $match: {_id: ObjectId(req.user._id)}
    },
    {
        $lookup:{
            "from": "trips",
            "let": { "trip": "$request_rec.trip_id" },
            "pipeline": [
                    { "$match": {
                                "$expr": { "$eq": ["$$trip", "$_id"] }
                        }
                    },
                    {
                        $lookup:{
                            "from": "tripjoinrequests",
                            "let": { "trip": "$_id","admin": ObjectId(req.user['_id']) },
                            "pipeline": [
                                    { "$match": {
                                        "$expr":{
                                            $and:[
                                                { "$eq": ["$$trip", "$trip"] },
                                                { "$eq": ["$$admin", "$admin"]}
                                            ]}
                                        }
                                    },
                                    { "$project": { 'name': 1, 'dob': 1, 'country': 1,'avatar': 1 }}
                                ],
                            "as": "pending"

                        }
                    },

                    { "$project": { "location": 1, "date": 1, "pending": 1 }}
                ],
            "as": "request_rec"
        }
    },
    {
        $lookup:{
            "from": "trips",
            "let": { "trip": "$request_send.trip_id" },
            "pipeline": [
                    { "$match": {
                                "$expr": { "$eq": ["$$trip", "$_id"] }
                        }
                    },
                    {
                        $lookup:{
                            "from": "users",
                            "let": { "admin":  "$admin" },
                            "pipeline": [
                                    { "$match": {
                                        "$expr":
                                                { "$eq": ["$$admin", "$_id"] },
                                        }
                                    },
                                    { "$project": { 'name': 1, 'dob': 1, 'country': 1,'avatar': 1 }}
                                ],
                            "as": "admin"

                        }
                    },
                    { "$unwind": "$admin"},
                    { "$project": { "location": 1, "date": 1,"admin": 1 }}
                ],
            "as": "request_send"
        }
    },
    {
        $lookup:{
            "from": "trips",
            "let": { "tripId": "$upcomming_trips.trip_id" },
            "pipeline": [
                    { "$match": {
                                "$expr": { "$eq": ["$$tripId", "$_id"] }
                        }
                    },
                {
                        $lookup:{
                            "from": "users",
                            "let": { "adminId":  "$admin" },
                            "pipeline": [
                                    { "$match": {
                                        "$expr":
                                                { "$eq": ["$$adminId", "$_id"] },
                                        }
                                    },
                                    { "$project": { 'name': 1, 'dob': 1, 'country': 1,'avatar': 1 }}
                                ],
                            "as": "Admin"

                        }
                    },
                    {$unwind: "$Admin"},
                    {
                        $lookup:{
                            "from": "users",
                            "let": { "memberId":  "$members.user_id" },
                            "pipeline": [
                                    { "$match": {
                                        "$expr":
                                                { "$eq": ["$$memberId", "$_id"] },
                                        }
                                    },
                                    { "$project": { 'avatar': { "$slice": [ "$avatar", 3 ]}, "_id": -1}}
                                ],
                            "as": "Members"
                        }
                    },
                    { "$project": { "location": 1, "date": 1,"Admin": 1,"memberCount": 1, "coordinates": 1,'Members': 1 }}
                ],
            "as": "Upcomming_trips"
        }
    },

    {
        $lookup:{
            "from": "trips",
            "let": { "trip": "$completed_trips.trip_id" },
            "pipeline": [
                    { "$match": {
                                "$expr": { "$eq": ["$$trip", "$_id"] }
                        }
                    },
                                            {
                        $lookup:{
                            "from": "users",
                            "let": { "member":  "$members.user_id" },
                            "pipeline": [
                                    { "$match": {
                                        "$expr":
                                                { "$eq": ["$$member", "$_id"] },
                                        }
                                    },
                                    { "$project": { 'avatar': { "$slice": [ "$avatar", 3 ]}, "_id": -1}}
                                ],
                            "as": "members"
                        }
                    },
                    { "$project": { "location": 1, "date": 1, "memberCount": 1,'members': 1 }}
                ],
            "as": "completed_trips"
        }
    },

    { $unwind: {path: "$Upcomming_trips", "preserveNullAndEmptyArrays": true}},
    { $unwind: {path: "$request_rec", "preserveNullAndEmptyArrays": true}},
    { $unwind: {path: "$request_send", "preserveNullAndEmptyArrays": true}},
    { $unwind: {path: "$completed_trips", "preserveNullAndEmptyArrays": true}},
    {
        "$project":{
            "request_rec": 1,
            "request_send": 1,
            "Upcomming_trips": 1,
            "completed_trips": 1
        }
    }
]).exec(function(err, mytrips){
    if(err) return res.status(422).send({error: err.message});
    console.log("mytrips",JSON.stringify(mytrips,null,4))
    res.status(200).send({
        mytrips: mytrips
    });
})

User model

{ "_id" : ObjectId("5fafe5e06b3f1d6259406697"), "show_onboarding" : false, "role" : "Normal", "marketing_emails" : true, "account_activated" : true, "galleryUrls" : [ ], "spokenLanguages" : [ ], "countriesVisited" : [ ], "tripsCount" : 0, "name" : "Kj Jose", "email" : "jose2007kj@gmail.com", "salt" : "982956249717", "hashed_password" : "7d9f29e08be931bf1c2a4a2224ff25a1f96c6a82", "avatar" : "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=2314576115355640&height=50&width=50&ext=1607955339&hash=AeSxNozVpV_6lIf85lc", "request_rec" : [ ], "request_send" : [ ], "upcomming_trips" : [ { "_id" : ObjectId("5fafe60c6b3f1d6259406699"), "trip_id" : ObjectId("5fafe60c6b3f1d6259406698"), "createdAt" : ISODate("2020-11-14T14:13:32.782Z") }, { "_id" : ObjectId("5fafe7606b3f1d625940669b"), "trip_id" : ObjectId("5fafe7606b3f1d625940669a"), "createdAt" : ISODate("2020-11-14T14:19:12.013Z") }, { "_id" : ObjectId("5fb0072a2f5fb26e977bb1a1"), "trip_id" : ObjectId("5fb0072a2f5fb26e977bb1a0"), "createdAt" : ISODate("2020-11-14T16:34:50.728Z") } ], "completed_trips" : [ ], "createdAt" : ISODate("2020-11-14T14:12:48.200Z"), "updatedAt" : ISODate("2020-11-15T21:54:55.723Z"), "__v" : 0, "fcmToken" : "frAT3vq8Ri2k2v7Fms-JHa:APA91bHD775-exOoT3wbJYJdoe3aT53dZ7RFW-q4gUfq0mrIbb84vd5YmcVPHpjGwnoGmKKZz0hyIJnrW3gq7zOFm-w71kjQU5IP1IrVMKepUNqtCtbRmQTqY6hvmoKvn6gXezzWp7Op" }

trips model

{ "_id" : ObjectId("5fafe7606b3f1d625940669a"), "coordinates" : [ 101.71649, 3.10718 ], "open" : true, "completed" : false, "memberCount" : 1, "location" : "cheras, kuala lumpur, malaysia", "date" : ISODate("2020-11-14T00:00:00Z"), "desc" : "gtgtgtgtgtg", "admin" : ObjectId("5fafe5e06b3f1d6259406697"), "members" : [ ], "createdAt" : ISODate("2020-11-14T14:19:12.013Z"), "updatedAt" : ISODate("2020-11-14T14:19:12.013Z"), "__v" : 0 }

{ "_id" : ObjectId("5fb0072a2f5fb26e977bb1a0"), "coordinates" : [ 101.71649, 3.10718 ], "open" : true, "completed" : false, "memberCount" : 1, "location" : "cheras, kuala lumpur, malaysia", "date" : ISODate("2020-12-25T00:00:00Z"), "desc" : "huhhuhuhu", "admin" : ObjectId("5fafe5e06b3f1d6259406697"), "members" : [ ], "createdAt" : ISODate("2020-11-14T16:34:50.728Z"), "updatedAt" : ISODate("2020-11-14T16:34:50.728Z"), "__v" : 0 }

{ "_id" : ObjectId("5fb0180ae58c397e9d630c1d"), "coordinates" : [ -0.5074, 51.3902 ], "open" : true, "completed" : false, "memberCount" : 1, "location" : "chertsey, surrey, england, united kingdom", "date" : ISODate("2020-12-25T00:00:00Z"), "desc" : "iyiyiyiyiiy", "admin" : ObjectId("5fafe5e06b3f1d6259406697"), "members" : [ ], "createdAt" : ISODate("2020-11-14T17:46:50.999Z"), "updatedAt" : ISODate("2020-11-14T17:46:50.999Z"), "__v" : 0 }
{ "_id" : ObjectId("5fb1a3afa3c3bc24908b42ec"), "coordinates" : [ 77.05972, 10.08917 ], "dob" : ISODate("2004-11-15T21:52:53.564Z"), "open" : true, "completed" : false, "memberCount" : 1, "location" : "munnar, kerala, india", "date" : ISODate("2020-12-25T00:00:00Z"), "desc" : "hahahahhah", "admin" : ObjectId("5fafe5e06b3f1d6259406697"), "members" : [ ], "createdAt" : ISODate("2020-11-15T21:54:55.680Z"), "updatedAt" : ISODate("2020-11-15T21:54:55.680Z"), "__v" : 0 }

tripjoinrequest data

{ "_id" : ObjectId("5f91b92bcdec9c3b5146973d"), "status" : "Accepted", "trip_id" : ObjectId("5f8fe98d0259331ab7eae568"), "message" : "tetetetetet", "user" : ObjectId("5f53214e2893f20ae9e6924d"), "createdAt" : ISODate("2020-10-22T16:54:03.253Z"), "updatedAt" : ISODate("2020-10-26T09:57:37.294Z"), "__v" : 0 }
{ "_id" : ObjectId("5f992ada54e28d25e53decd2"), "status" : "Accepted", "message" : "tetetettetetetetettettettetetetetetteet", "trip_id" : ObjectId("5f99247a54e28d25e53decd0"), "user" : ObjectId("5f9927a954e28d25e53decd1"), "createdAt" : ISODate("2020-10-28T08:24:58.187Z"), "updatedAt" : ISODate("2020-10-31T11:25:51.009Z"), "__v" : 0 }
{ "_id" : ObjectId("5f9aa9a30a3b160fe85fa695"), "status" : "Accepted", "message" : "I would like to join", "trip_id" : ObjectId("5f9aa8af0a3b160fe85fa694"), "user" : ObjectId("5f53214e2893f20ae9e6924d"), "createdAt" : ISODate("2020-10-29T11:38:11.739Z"), "updatedAt" : ISODate("2020-10-29T11:39:37.743Z"), "__v" : 0 }
{ "_id" : ObjectId("5f9d4975775eb52d57209e08"), "status" : "Accepted", "message" : "I wanna join", "trip_id" : ObjectId("5f9d4836775eb52d57209e07"), "user" : ObjectId("5f9927a954e28d25e53decd1"), "createdAt" : ISODate("2020-10-31T11:24:37.729Z"), "updatedAt" : ISODate("2020-10-31T11:25:45.027Z"), "__v" : 0 }
{ "_id" : ObjectId("5fb1aaac6e1d2329cefa6bc4"), "message" : "jhjhjhjhjhj", "admin" : ObjectId("5fb034675a4ee09507af0055"), "user" : ObjectId("5fafe5e06b3f1d6259406697"), "createdAt" : ISODate("2020-11-15T22:24:44.822Z"), "updatedAt" : ISODate("2020-11-15T22:24:44.822Z"), "__v" : 0 }

mongodb version 4.4. please guide me. where i might be going wrong i was expecting upcomming_trips details to be there in output.

Thanks

await User.aggregate([
    {
        $match: {_id: ObjectId(req.user._id)}
    },
    {$unwind: {path: "$upcomming_trips", preserveNullAndEmptyArrays: true }},
    {
        $lookup:{
            "from": "trips",
            "let": { "tripId": "$upcomming_trips.trip_id" },
            "pipeline": [
                    { "$match": {
                                "$expr": { "$eq": ["$$tripId", "$_id"] }
                        }
                    },
                    {
                        $lookup:{
                            "from": "users",
                            "let": { "adminId":  "$admin" },
                            "pipeline": [
                                    { "$match": {
                                        "$expr":
                                                { "$eq": ["$$adminId", "$_id"] },
                                        }
                                    },
                                    { "$project": { 'name': 1, 'dob': 1, 'country': 1,'avatar': 1 }}
                                ],
                            "as": "Admin"

                        }
                    },
                    {$unwind: "$Admin"},
                    {
                        $lookup:{
                            "from": "users",
                            "let": { "memberId":  "$members.user_id" },
                            "pipeline": [
                                    { "$match": {
                                        "$expr":
                                                { "$eq": ["$$memberId", "$_id"] },
                                        }
                                    },
                                    { "$project": { 'avatar': { "$slice": [ "$avatar", 3 ]}, "_id": -1}}
                                ],
                            "as": "Members"
                        }
                    },
                    { "$project": { "location": 1, "date": 1,"Admin": 1,"memberCount": 1, "coordinates": 1,'Members': 1 }}
                ],
            "as": "upcomming_trips1"
        }
    },
    {$unwind: {path: "$upcomming_trips1", preserveNullAndEmptyArrays: true }},

    {$unwind: {path: "$request_rec", preserveNullAndEmptyArrays: true }},        {
        $lookup:{
            "from": "trips",
            "let": { "trip": "$request_rec.trip_id" },
            "pipeline": [
                    { "$match": {
                                "$expr": { "$eq": ["$$trip", "$_id"] }
                        }
                    },
                    {
                        $lookup:{
                            "from": "tripjoinrequests",
                            "let": { "trip": "$_id","admin": ObjectId(req.user['_id']) },
                            "pipeline": [
                                    { "$match": {
                                        "$expr":{
                                            $and:[
                                                { "$eq": ["$$trip", "$trip"] },
                                                { "$eq": ["$$admin", "$admin"]}
                                            ]}
                                        }
                                    },
                                    { "$project": { 'name': 1, 'dob': 1, 'country': 1,'avatar': 1 }}
                                ],
                            "as": "pending"

                        }
                    },

                    { "$project": { "location": 1, "date": 1, "pending": 1 }}
                ],
            "as": "request_rec"
        }
    },
    {$unwind: {path: "$request_rec", preserveNullAndEmptyArrays: true }},

    {$unwind: {path: "$request_send",preserveNullAndEmptyArrays: true }},        {
        $lookup:{
            "from": "trips",
            "let": { "trip": "$request_send.trip_id" },
            "pipeline": [
                    { "$match": {
                                "$expr": { "$eq": ["$$trip", "$_id"] }
                        }
                    },
                    {
                        $lookup:{
                            "from": "users",
                            "let": { "admin":  "$admin" },
                            "pipeline": [
                                    { "$match": {
                                        "$expr":
                                                { "$eq": ["$$admin", "$_id"] },
                                        }
                                    },
                                    { "$project": { 'name': 1, 'dob': 1, 'country': 1,'avatar': 1 }}
                                ],
                            "as": "admin"

                        }
                    },
                    { "$unwind": "$admin"},
                    { "$project": { "location": 1, "date": 1,"admin": 1 }}
                ],
            "as": "request_send"
        }
    },
    {$unwind: {path: "$request_send",preserveNullAndEmptyArrays: true }},

    {$unwind: {path:"$completed_trips", preserveNullAndEmptyArrays: true }},
    {
        $lookup:{
            "from": "trips",
            "let": { "trip": "$completed_trips.trip_id" },
            "pipeline": [
                    { "$match": {
                                "$expr": { "$eq": ["$$trip", "$_id"] }
                        }
                    },
                                            {
                        $lookup:{
                            "from": "users",
                            "let": { "member":  "$members.user_id" },
                            "pipeline": [
                                    { "$match": {
                                        "$expr":
                                                { "$eq": ["$$member", "$_id"] },
                                        }
                                    },
                                    { "$project": { 'avatar': { "$slice": [ "$avatar", 3 ]}, "_id": -1}}
                                ],
                            "as": "members"
                        }
                    },
                    { "$project": { "location": 1, "date": 1, "memberCount": 1,'members': 1 }}
                ],
            "as": "completed_trips"
        }
    },
    {$unwind: {path: "$completed_trips",preserveNullAndEmptyArrays: true }},
    {
        $group: {
            "_id": "$_id" ,
            'upcomming_trips': { $push: "$upcomming_trips1"},
            'request_rec': {$push: "$request_rec"},
            'request_send': {$push: "$request_send"},
            'completed_trips': {$push: "$completed_trips"}
        }
    },


    {
        "$project":{
            "request_rec": 1,
            "request_send": 1,
            "upcomming_trips": 1,
            "completed_trips": 1
        }
    }
]).exec(function(err, mytrips){
    if(err) return res.status(422).send({error: err.message});
    console.log("mytrips",JSON.stringify(mytrips,null,4))
    res.status(200).send({
        mytrips: mytrips
    });
})

I finally got it working with this query ,Thanks
please let me know if the query is optimal,