kra
kra

Reputation: 19

Query data stored in MongoDB by date & time

I want to count the number of objects which were stored in the MongoDB after a certain time. Date is stored in MongoDB in following format.

 "clickTime" : ISODate("2016-07-09T07:17:29.932Z")

I wrote the following code to count the objects but it is giving me _count = 0, while it should give me _count = 1. I am doing this in Django

def Count(request):
    time_from = datetime.time(0, 0, 0)

    ## today's count
    d1 = datetime.date.today()
    start1 = datetime.datetime.combine(d1,time_from)
    end1 = datetime.datetime.now()
    count1 = utils.Processor1(collect,start1,end1)

this is my utils file

 def Processor1(collect,start,end):
   _count = collect.find({"clickTime":{"$gte":start,"$lte":end}}).count()
   return _count

Upvotes: 1

Views: 55

Answers (1)

DAXaholic
DAXaholic

Reputation: 35358

I am not quite sure of what type your start and end vars are according to your question but try it when setting them to datetime instances like below:

start = datetime.datetime(2016, 7, 9, 0, 0, 0, 0)
end = datetime.datetime(2016, 7, 9, 12, 21, 25, 366)

Upvotes: 1

Related Questions