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)]}
Haven't run your codes. Seems:
Should be: