1

I added a condition like below to an aggregation in mongodb.

"$match" : {                
$ifNull: ["$startDate", "$createdAt"] : {"$gte": ISODate("2018-06-01 10:03:46.000Z"), "$lte": ISODate("2018-06-29 10:03:46.000Z")}              
}

When I check startDate if it is undefined or null, i get createdAt but it shows me error when executing.

What is wrong in my query ?

Vega
  • 27,856
  • 27
  • 95
  • 103
HieuBui
  • 37
  • 1
  • 6
  • Your query is confusing. what exactly are you trying to do? what the outcome you expect? – abdul Jul 24 '18 at 08:02

1 Answers1

4

The first problem with your query is that your $match object is not a valid js object. You can not replace the object key with something like this. Also, there are other limitations with $match.

As stated in the mongodb match documentation

The $match query syntax is identical to the read operation query syntax; i.e. $match does not accept raw aggregation expressions. To include aggregation expression in $match, use a $expr query expression:

{ $match: { $expr: { <aggregation expression> } } }

$ifNull can be used in $match like this;

{ 
  $match: { 
    $expr: { 
      $gte: [
          {$ifNull: ["$startDate", "$createdAt"]},
          ISODate("2018-06-01 10:03:46.000Z")
        ] 
    } 
  } 
}

Possible versions of your query:##

Using only $match:

{ 
  $match: { 
    $expr: { 
      $and: [
        { $gte: [
          {$ifNull: ["$startDate", "$createdAt"]},
          ISODate("2018-06-01 10:03:46.000Z")
        ]}, 
        { $lte: [
          {$ifNull: ["$startDate", "$createdAt"]},
          ISODate("2018-06-29 10:03:46.000Z")
        ]}
      ]
    } 
  } 
}

Alternatively, you can use $project to compute the value and use in later steps

NOTE: This will affect performance, as the Mongo won't be able to use index while matching data.

{
  $project: {
    'computedStartDate' : {$ifNull: ["$startDate", "$createdAt"]}
  }
},
{ 
  $match: { 
    "computedStartDate": 
      {"$gte": ISODate("2018-06-01 10:03:46.000Z"), "$lte": ISODate("2018-06-29 10:03:46.000Z")
    } 
  } 
}
kubak
  • 163
  • 1
  • 8
hknkrt
  • 96
  • 4
  • Awesome answer! It's worth noting that the second variant with `$project` will perform much worse because it won't use an index (if there's one on `$startDate` and `$createdAt`) – kubak Jul 27 '21 at 09:11