Home > Mobile >  Is there a way to query on parts of a date in a `find` query in mongodb?
Is there a way to query on parts of a date in a `find` query in mongodb?

Time:02-28

so I know I can do this with an aggregate operation, but I'm not sure if I'm missing something as I can't seem to do it with a simple find call. For example I tried:

{'CreationTime': { '$dayOfMonth': 26 }}

But that failed with: unknown operator: $dayOfMonth.

I think I can achieve this with an $in query but that feels ugly having to generate every possible date.

Is this possible with a regular find query?

CodePudding user response:

Short answer: No it is not.

find() only supports (as of v5) a small subset of functions that can be applied to fields compared to aggregate(). For some years now, aggregate is the new find. Under almost all conditions there is no performance benefit to using find but for simple expressions it is clearly more compact and convenient.

And starting with v4.4, many tricky update expressions are handled easier through aggregate with the merge stage set to the same collection as the input.

  • Related