Retrieve data from mongodb based on date condition in pymongo

一个人想着一个人 提交于 2020-01-15 13:21:05

问题


My MongoDB document is as follows: -

{
    "_id" : 111,
    "ts" : [ 
    ISODate("2010-08-21T23:13:35.000Z"), 
    ISODate("2011-08-23T09:13:10.000Z"), 
    ISODate("2012-08-26T12:55:51.000Z"), 
    ISODate("2010-08-29T03:11:12.000Z")
]
}

{
    "_id" : 112,
    "ts" : [ 
    ISODate("2010-08-21T23:13:35.000Z"), 
    ISODate("2011-08-23T09:13:10.000Z"), 
    ISODate("2012-08-26T12:55:51.000Z"), 
    ISODate("2010-08-29T03:11:12.000Z")
]

}

Each date entry indicates a "hit". I am trying to count the number of hits each id had between a certain date period.

Query looks like :-

db.pgview.aggregate
({$match:{ts:{$elemMatch:{$gte:ISODate("2012-09-01"),$lte:ISODate("2014-09-01")}}}},
 {$unwind:"$ts"},
 {$match:{ts:{$gte:ISODate("2012-09-01"),$lte:ISODate("2014-09-01")}}},
 {$group:{_id:"$_id",count:{$sum:1}}})

This works fine on Mongo Shell. But I am trying to retrieve the same from Python for which I am not getting any values. Python code :-

def __init__(self):

    self.conn = pymongo.MongoClient()["test"]
    self.coll = self.conn.pgview
    self.stDate = datetime.datetime.now()
    self.endDate = datetime.datetime.now() - datetime.timedelta(days=1000)
    print self.stDate
    print self.endDate

def fetchData(self):
    text = self.coll.aggregate([{"$match":{"ts":{"$elemMatch":{"$gte":self.stDate,"$lte":self.endDate}}}},
                {"$unwind":"$ts"},
                {"$match":{"ts":{"$gte":self.stDate,"$lte":self.endDate}}},
                {"$group":{"_id":"$_id","count":{"$sum":1}}}])
    jsondata = text["result"]
    for each in jsondata:
        try:
            print each  
        except Exception, err:
            print traceback.format_exc()        

But it does not return any results. Can anyone please help? Is it because of date format?

UPDATE

I confirmed that the type that is retrieved from mongodb is datetime The way it is returned if i query without any condition is

{u'_id': 111, u'ts': [datetime.datetime(2010, 8, 21, 23, 13, 35), datetime.datetime(2011, 8, 23, 9, 13, 10), datetime.datetime(2012, 8, 26, 12, 55, 51), datetime.datetime(2010, 8, 29, 3, 11, 12)]}

回答1:


Haven't run your codes. Seems:

self.stDate = datetime.datetime.now()
self.endDate = datetime.datetime.now() - datetime.timedelta(days=1000)

Should be:

self.stDate = datetime.datetime.now() - datetime.timedelta(days=1000)
self.endDate = datetime.datetime.now()


来源:https://stackoverflow.com/questions/26989429/retrieve-data-from-mongodb-based-on-date-condition-in-pymongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!