Wednesday, 10 May 2017

How to make $lookup in aggregate return one object not an array

when i call $lookup it return array but my requirment in only one object. is there any way where i'll get only one object. i have to put $lookup value in $group.

 Message.aggregate(

            {$match:{$or:[{"sender":userId},{"receiver":userId}]}},          
            { $lookup: {from: 'users', localField: 'senderId', foreignField: '_id', as: 'from'} },

            {$sort:{messageTimeStamp:-1}},
            {
                $group:{"_id":
                {
                  "name":
                      }
                     $cond:[
                         {
                             $eq:["$receiver",userId]
                         },
                         {$concat:["$receiver"," and ","$sender"]},
                         {$concat:["$sender"," and ","$receiver"]}
                     ]
                  }
                },"message":{$first:"$message"},
                   "time":{$first:"$messageTimeStamp"},
                   "name":{$first:{
                     $cond:[
                         {
                              $eq:['$receiver',userId]
                        },
                         "$user1",
                         "$user2
                     ]
                  }},
                }
            },



via pank sharma

No comments:

Post a Comment