user2863110
user2863110

Reputation: 21

Query DateTime with PyMongo

In my database i have "createTime" value like this :

{
    "_id" : ObjectId("5744041be1db9cc2fc942660"),
    "_class" : "vn.cdt.entity.db.AccessLog",
    "url" : "/bo-trang-diem/bo-phan-trang-diem-make-up-kit-a-d-s-208242816346",
    "userAgent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36",
    "ip" : "0:0:0:0:0:0:0:1",
    "sessionId" : "478E67E43DDDF1CE9281C366EC124126",
    "oldSessionId" : "E5F38900137FAB4E8C3525DBBAF21538",
    "cookie" : "{\"sessionId\":\"E5F38900137FAB4E8C3525DBBAF21538\",\"objects\":[{\"id\":\"208242816346\",\"type\":\"VIEW_ITEM\",\"count\":2}]}",
    "isCookie" : true,
    "createTime" : NumberLong(1464075049835),
    "objectId" : "208242816346",
    "name" : "Bộ phấn trang điểm Make up Kit A.D.S",
    "type" : "VIEW_ITEM"
}

Focus on "createTime", that mean

"GMT: Tue, 24 May 2016 07:30:49.835 GMT"

Now i want write python code with PyMongo that will print all records in one day from that day (from 24/May/2016 to 25/May/2016). I read document of PyMongo and try some code : MongoDB / Pymongo Query with Datetime but not success. I hope you can help me


Thanks very much !

Upvotes: 0

Views: 4930

Answers (1)

Atish
Atish

Reputation: 4417

Using pymongo this can be done using:

from datetime import date, timedelta

db.collection.find({'createTime':{'$lt':datetime.datetime.now(), '$gt':datetime.datetime.now() - timedelta(hours=24)}})

Upvotes: 2

Related Questions