0

I was always convinced that these queries are equivalent:

{ 'since.date': { '$lte': ISODate('2018-07-11T09:00:00.000Z') } }

,

{ 'since': { 'date': { '$lte': ISODate('2018-07-11T09:00:00.000Z') } } }

However, only the first one returns the object I was looking for - the second one fetches no records.

What's the catch here?

Michal
  • 459
  • 2
  • 7
  • 25

1 Answers1

1

The only proper way to perform $lte query on nested object is using the dot notation (docs here)

Your second query simply checks if the document has specified structure, so it will return only following document:

{ "since" : { "date" : { "$lte" : ISODate("2018-07-11T09:00:00Z") } } }

but it won't return documents like:

{ "since" : { "date" : ISODate("2018-07-11T09:00:00Z") } }
{ "since" : { "date" : { "a" : 3, "$lte" : ISODate("2018-07-11T09:00:00Z") } } }

There's no dates comparison here, it just compares entire document structure. You should never use this syntax.

mickl
  • 48,568
  • 9
  • 60
  • 89