使用pyMongo创建一个ISODate [英] Create an ISODate with pyMongo
问题描述
我一直在尝试找到一种通过pyMongo客户端创建ISODate对象的方法,但到目前为止没有成功.
I've been trying to find a way to create an ISODate object whith pyMongo client, but without any success so far.
我使用 http://pypi.python.org/pypi/pymongo3 客户端,这是唯一的严重客户端暂时在Python 3中可用,但问题似乎不在于此特定的pymongo版本.
I use http://pypi.python.org/pypi/pymongo3 client, which is the only serious one available in Python 3 for now, but the problem doesn't seem to come from this specific pymongo version.
我想知道是否有人从pymongo客户端找到了使用此MongoDB对象类型的解决方案...谢谢您的帮助!
I'd like to know if any of you has found a solution to use this MongoDB object type from a pymongo client... thanks for your help !
推荐答案
您只需要存储datetime.datetime的实例.
You just need to store an instance of datetime.datetime.
从python shell插入:
Inserting from the python shell:
>>> c.test.test.insert({'date': datetime.datetime.utcnow()})
ObjectId('4e8b388367d5bd2de0000000')
>>> c.test.test.find_one()
{u'date': datetime.datetime(2011, 10, 4, 16, 46, 59, 786000), u'_id': ObjectId('4e8b388367d5bd2de0000000')}
在mongo shell中查询:
Querying in the mongo shell:
> db.test.findOne()
{
"_id" : ObjectId("4e8b388367d5bd2de0000000"),
"date" : ISODate("2011-10-04T16:46:59.786Z")
}
这篇关于使用pyMongo创建一个ISODate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!