1

I have a collection with documents containing a date.

Is there a way to find all documents from a particular month (all years), using db.myCollection.find()?

I managed to achieve approximately what I need by using the aggregation pipeline and the $month operator, but I couldn't find anything similar among the 'query and projection' operators.

I do not need to group the documents (I only need to filter them).

I am modifying some code that creates filters dynamically based on the user selection and the code uses find with the generated filters.

I would like to be able to give a user the ability to see only entries from particular months (say, only entries for the summer season).

Finally is there a performance difference between using a filter with find and using the equivalent filter in a $match stage with the aggregation pipeline ?

raduw
  • 1,008
  • 1
  • 9
  • 19
  • findMany? No such method. [`$redact`](https://docs.mongodb.com/manual/reference/operator/aggregation/redact/) would be preferable with aggregation since it's a "conditional filter". But if you really need to do this on a regular basis, then instead you should "store" the month on the document in addition to the date. Then you just query on the "month" property that is already there. – Neil Lunn Jun 16 '17 at 00:14
  • 1
    You are basically asking another variation of the "birthday problem", which essentially is looking for people born on "this day" based on the stored date of their birth. Just like there, the efficient way is to store the "parts" of the date, rather than try to process on the server. – Neil Lunn Jun 16 '17 at 00:19
  • Sure, my mistake the mongo api is `find` not `findMany` ( I'm using it thorugh an api layer that has findMany and findOne which returns a container or an element).- I corrected the question. – raduw Jun 16 '17 at 07:05
  • 1
    I basically answered it. You "should" store the month individually. If you are not prepared to do that then use `.aggregate()` with `$redact` and the `$month` aggregation operator to extract the month and compare. – Neil Lunn Jun 16 '17 at 07:18
  • Thanks Neil, I wanted to give you credit for the answer but I can't do that on a comment (it should be an answer in order to mark it as answered). – raduw Jun 16 '17 at 09:10

0 Answers0