Perform a conditional lookup, only when the localfield exists?

Viewed 15368

I have a collection, which contains two types of documents. Based on the type of document I want to perform lookups aggregation.

Action is a collection, which contains both conversations and message type of actions. I want to perform a lookup for conversation and messages differently based on the fields present in MongoDB.

The action document for conversation type is as follows

{ 
    "_id" : ObjectId("592bdeaf45c7201421793871"), 
    "actionType" : "conversation", 
    "created" : NumberInt(1496047280), 
    "users" : [
        ObjectId("590c53a85fba594a59fe3d0f"), 
        ObjectId("590c50175df715499129e41b")
    ], 
    "conversationId" : ObjectId("592bdeaf45c7201421793870"), 
    "missionId" : ObjectId("590c50fa5df715499129e41c")
}

Action document for message

{ 
    "_id" : ObjectId("592bdeaf45c7201421793871"), 
    "actionType" : "message", 
    "created" : NumberInt(1496047280), 
    "messageId" : ObjectId("592bdeaf45c7201421793870")
}

I want actions after a particular time and want to perform lookups to get the conversation and message data from their object Ids.

I tried this

let matchQuery = {
          users : new ObjectId(userId),
          created:
            {
              $gt : createdTime
            }};

       let aggregation = [
          {
            $match : matchQuery
          },
          {$cond : 
            {if:
              {
                $users:{
                  $exists:true
                }
            }, 
          then: {
            $unwind : "$users",
            $lookup: {
                 from : "users",
                 localfield:"$users",
                 foreignfield:"_id",
                 as:"userDetail"
                       },
            $group:{
                  "users" : { $push : "userDetail"}
                    }
                  }
              }
            },
            {$cond : 
            {if:
              {
                $conversationId:{
                  $exists:true
                }
            }, 
          then: {
           $lookup : {
                            from:"conversations",
                            localfield:"conversationId",
                            foreignfield:"_id",
                            as:"conversationDetail"
                          }
                  }
              }
            },
            {$cond : 
            {if:
              {
                $missionId:{
                  $exists:true
                }
            }, 
          then: {
           $lookup : {
                            from:"services_v2",
                            localfield:"missionId",
                            foreignfield:"_id",
                            as:"missionDetail"
                          }
                  }
              }
            },
            {$cond : 
            {if:
              {
                $messageId:{
                  $exists:true
                }
            }, 
          then: {
           $lookup : {
                            from:"messagev2",
                              localfield:"messageId",
                              foreignfield:"_id",
                              as:"messageDetail"
                          }
                  }
              }
            },
          {
            $project : {
              "_id" : 1,
              "actionType" : 1,
              "userDetail":1,
              "conversationDetail":1,
              "missionDetail":1,
              "messageDetail":1
}}
        ];

connection.collection('actions')
.aggregate(aggregation).toArray((err,result)=> {  
    if(err){
      console.log(err);
    }
    console.log(result);
 })
};
1 Answers
Related