highwind
highwind

Reputation: 11

How to query multi date(day in history) in mongodb

I am trying to build my own blog program one function is to let me check the article post in this day of history eg. today is 2021-02-20 then the page will show the articles post on 2020-02-20, 2020-01-20, ..., 2019-02-20 and so on.

I just figured out that to set a start date and an end date to query each range of the articles(in python):

date_start = datetime(datetime.now().year, datetime.now().month, post_day, 0,0,0,0)
date_end = datetime(datetime.now().year, datetime.now().month, post_day, 23,59,59,999)
posts = DataPost.objects((Q(created_at__gte=date_start) & Q(created_at__lte=date_end)))\
        .order_by('-created_at').paginate(page=page, per_page=3)

but how to query multiple date, do I have to query multiple times in a for loop and combine the results? Or is there a much smarter way?

Thanks

Upvotes: 0

Views: 20

Answers (1)

highwind
highwind

Reputation: 11

As I use mongoengine, I tried this one:

 cond0 = Q()...
 for loop:
   cond1 = Q(...)
   cond0 = cond0 | cond1

 DataPost.objects(cond0)

Although it works, but not seems perfect.

Upvotes: 1

Related Questions