title: Perform a conditional lookup, only when the localfield exists?
body: <p>I have a collection, which contains two types of documents. Based on the type of document I want to perform lookups aggregation.</p>
  
  <p>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.</p>
  
  <p>The action document for conversation type is as follows</p>
  
  <pre><code>{ 
      "_id" : ObjectId("592bdeaf45c7201421793871"), 
      "actionType" : "conversation", 
      "created" : NumberInt(1496047280), 
      "users" : [
          ObjectId("590c53a85fba594a59fe3d0f"), 
          ObjectId("590c50175df715499129e41b")
      ], 
      "conversationId" : ObjectId("592bdeaf45c7201421793870"), 
      "missionId" : ObjectId("590c50fa5df715499129e41c")
  }
  </code></pre>
  
  <p>Action document for message </p>
  
  <pre><code>{ 
      "_id" : ObjectId("592bdeaf45c7201421793871"), 
      "actionType" : "message", 
      "created" : NumberInt(1496047280), 
      "messageId" : ObjectId("592bdeaf45c7201421793870")
  }
  </code></pre>
  
  <p>I want actions after a particular time and want to perform lookups to get the conversation and message data from their object Ids. </p>
  
  <p>I tried this </p>
  
  <pre><code>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)=&gt; {  
      if(err){
        console.log(err);
      }
      console.log(result);
   })
  };
  </code></pre>