0

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")
        }
    }
}]
Yann Masoch
  • 1,628
  • 1
  • 15
  • 20
  • In the match condition { value: {$gte: 600}, datetime: { $gte: ISODate("2019-02-01T00:00:00Z"), $lt: ISODate("2019-04-10T23:59:59Z") } }, are you getting document with user_id: ObjectId("5c9168ec5530c90d0c5cd98a") ? – Prashanti Apr 17 '19 at 21:58
  • Yes, for this query I am getting all the documents in between the dates including those relative to user_id: ObjectId("5c9168ec5530c90d0c5cd98a") – Yann Masoch Apr 17 '19 at 23:35
  • What version of compass are you using? – Mani Apr 18 '19 at 04:25
  • It's version 1.16.3 - I should try to install the latest version (1.17.x) to see if there is a difference. – Yann Masoch Apr 18 '19 at 04:32

1 Answers1

1

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") }
}
Yann Masoch
  • 1,628
  • 1
  • 15
  • 20