0

I am trying to write a mongo query in python to get the monthly and weekly range from days. So far I have

   my_collection.find({
      'date':{
        '$gte': date_start,
        '$lte': date_end
      }
}

It returns the day range between date_start and date_end, I tried to use '$month': 'gte', 'lte' but is not working, does anyone has any ideas how to get monthly and weekly based on the start date and end? The date_start and date_end are datetime objects.

Example ( date_start = 2017-01-01, date_end = 2017-12-01) I want to return just the month between this two range(start, end): 01, 02, 03, 04, 05, 06 ..

I just went through the same examples that has been asked before and what I asked is different, in my situation I have two dates, start and end.. now from this two dates I just want to display the month by combining the two dates together.. so far I haven't seen an example of how to do it.

Thanks

Mihail
  • 385
  • 1
  • 6
  • 17
  • how are you keeping your data?? please add some explanation about your objects in the database, i.e. add a sample data objects and the result you want from the query, you question seems a bit unclear – ganjim Nov 23 '18 at 14:56
  • The data is saved in mongodb, an example of the data is : datetime.datetime(2017, 2, 8, 10, 0, 25). Result that I want: example ( date_start = 2017-01-01, date_end = 2017-12-01) I want to return just the month between this two range(start, end). – Mihail Nov 23 '18 at 14:58
  • `$month` is not something you should use in a `$match` expression. The best advice it keep the "range query" in the match, and then simply `$project` the using `$month` to you have that value. Attempting something like "select november for every year" is really bad for performance, and if that was your intent then you are better off storing "month" within your document as separate field. Plenty of examples of [`$month`](https://docs.mongodb.com/manual/reference/operator/aggregation/month/) usage, including documentation. – Neil Lunn Nov 23 '18 at 20:41
  • Neil I tried what you said and is not working: collection.find('$project': {'$month' : { '$gte': start, '$lte' : end }} – Mihail Nov 26 '18 at 09:48

0 Answers0