Yann Masoch
Yann Masoch

Reputation: 1630

MongoDB Compass Community 1.16.3 $match issue with ObjectId + ISODate

I am trying to build an aggregation on MongoDB Compass Community 1.16.3 and I have a strange issue on the $match stage querying ObjectId and ISODate at the same time.

The non working $match stage

{
  user_id: ObjectId("5c9168ec5530c90d0c5cd98a"),
  value: {$gte: 600},
  datetime: { $gte: ISODate("2019-02-01T00:00:00Z"), $lt: ISODate("2019-04-10T23:59:59Z") }
}

This query does not work at all and Compass return Expected end of input but "}" found.

enter image description here

But these $match stages work

{
  user_id: ObjectId("5c9168ec5530c90d0c5cd98a"),
  value: {$gte: 600}
}

Perfect result!

{
  value: {$gte: 600},
  datetime: { $gte: ISODate("2019-02-01T00:00:00Z"), $lt: ISODate("2019-04-10T23:59:59Z") }
}

Perfect result!

It seems that the query does not work if I use ObjectId and ISODate at the same time. So, did I made a mistake somewhere? Or do I have to split it in 2 $match stages? Any thoughts?

Edited

If I split the pipeline in 2 $match stages (I removed value in this example), it works well but I don't know if it is a good practice and if it is efficient!

[{
    $match: {
        user_id: ObjectId("5c9168ec5530c90d0c5cd98a")
    }
}, {
    $match: {
        datetime: {
            $gte: ISODate("2019-02-01T00:00:00Z"),
            $lt: ISODate("2019-04-01T00:00:00Z")
        }
    }
}]

Upvotes: 0

Views: 692

Answers (1)

Yann Masoch
Yann Masoch

Reputation: 1630

The issue comes from the version of MongoDB Compass Community I was using (1.16.3).

To solve it, update to the latest version or above: 1.17.0

Now, the following works perfectly!

{
  user_id: ObjectId("5c9168ec5530c90d0c5cd98a"),
  value: {$gte: 600},
  datetime: { $gte: ISODate("2019-02-01T00:00:00Z"), $lt: ISODate("2019-04-10T23:59:59Z") }
}

Upvotes: 1

Related Questions