skip to Main Content

Hi i am trying to query data from mongodb using python = 3.8, django=4.2.2, arrow==1.2.3 pymongo = 3.13.0, mongodb is serverless.

this is my filter object

filter['created_at']={
        '$gte': arrow.now(settings.LOCAL_TIME_ZONE).shift(hours=-24).span('day')[0].to(settings.TIME_ZONE).datetime,
        '$lt': arrow.now(settings.LOCAL_TIME_ZONE).span('day')[1].to(settings.TIME_ZONE).datetime,
        }

when i print filter[‘created_at’] i get output as

'created_at': {'$gte': datetime.datetime(2023, 11, 22, 18, 30, tzinfo=tzutc()), '$lt': datetime.datetime(2023, 11, 24, 18, 29, 59, 999999, tzinfo=tzutc())}

and this is my count function

def count(self, filters={}):
    q = self.collection.count_documents(filters)
    return q

I am not getting data when i pass filter[‘created_at’], and when i pass empty {} in filter i get count of documents and also mongodb is serverless.

Can anyone help me out! What is the issue is?

2

Answers


  1. I think there is nothing wrong with your code. Please try with a wide datetime range.

    Login or Signup to reply.
  2. If your object is indeed

    filter['created_at']={
            '$gte': arrow.now(settings.LOCAL_TIME_ZONE).shift(hours=-24).span('day')[0].to(settings.TIME_ZONE).datetime,
            '$lt': arrow.now(settings.LOCAL_TIME_ZONE).span('day')[1].to(settings.TIME_ZONE).datetime,
            }
    

    then print filter['created_at'] cannot produce the output you describe; the created_at key would be lost. Change your call to your count() function to pass filter, not filter['created_at'].

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search