如何获取 Mongodb 中特定时间戳的文档?
How to get a document for a particular timestamp in Mongodb?
我的 collection 中有以下一组文档信息以及如何获取具有特定时间戳的文档?
由于这个时间戳在 ISODate() 中,如何从我的 pyMongodb 客户端传递它?
[
{
"_id" : ObjectId("5f64658a64b79fed80b8e4df"),
"time" : ISODate("2020-09-18T13:15:14.290Z"),
"details" : "with all details",
"type" : "general",
"payLoad" : {
"myname" : "nameofme",
}
},
{
"_id" : ObjectId("5f64658a64b79fed80b8e4df"),
"time" : ISODate("2020-09-18T13:18:20.290Z"),
"details" : "with all details",
"type" : "general",
"payLoad" : {
"myname" : "nameofme",
}
},
{
"_id" : ObjectId("5f64658a64b79fed80b8e4df"),
"time" : ISODate("2020-09-18T13:50:20.290Z"),
"details" : "with all details",
"type" : "general",
"payLoad" : {
"myname" : "nameofme",
}
}
]
使用 datetime
对象构造它:
from datetime import datetime
from pymongo import MongoClient
db = MongoClient()['mydatabase']
dt = datetime(2020, 9, 18, 13, 15, 14, 290000)
print(list(db.mycollection.find({"time" : dt})))
给出:
[{'_id': ObjectId('5f64658a64b79fed80b8e4dd'), 'time': datetime.datetime(2020, 9, 18, 13, 15, 14, 290000), 'details': 'with all details', 'type': 'general', 'payLoad': {'myname': 'nameofme'}}]
我的 collection 中有以下一组文档信息以及如何获取具有特定时间戳的文档?
由于这个时间戳在 ISODate() 中,如何从我的 pyMongodb 客户端传递它?
[
{
"_id" : ObjectId("5f64658a64b79fed80b8e4df"),
"time" : ISODate("2020-09-18T13:15:14.290Z"),
"details" : "with all details",
"type" : "general",
"payLoad" : {
"myname" : "nameofme",
}
},
{
"_id" : ObjectId("5f64658a64b79fed80b8e4df"),
"time" : ISODate("2020-09-18T13:18:20.290Z"),
"details" : "with all details",
"type" : "general",
"payLoad" : {
"myname" : "nameofme",
}
},
{
"_id" : ObjectId("5f64658a64b79fed80b8e4df"),
"time" : ISODate("2020-09-18T13:50:20.290Z"),
"details" : "with all details",
"type" : "general",
"payLoad" : {
"myname" : "nameofme",
}
}
]
使用 datetime
对象构造它:
from datetime import datetime
from pymongo import MongoClient
db = MongoClient()['mydatabase']
dt = datetime(2020, 9, 18, 13, 15, 14, 290000)
print(list(db.mycollection.find({"time" : dt})))
给出:
[{'_id': ObjectId('5f64658a64b79fed80b8e4dd'), 'time': datetime.datetime(2020, 9, 18, 13, 15, 14, 290000), 'details': 'with all details', 'type': 'general', 'payLoad': {'myname': 'nameofme'}}]