简体   繁体   English

使用 pyMongo 创建 ISODate

[英]Create an ISODate with pyMongo

I've been trying to find a way to create an ISODate object whith pyMongo client, but without any success so far.我一直在尝试寻找一种方法来使用 pyMongo 客户端创建 ISODate object,但到目前为止没有任何成功。

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.我使用http://pypi.python.org/pypi/pymongo3客户端,这是目前 Python 3 中唯一可用的正经客户端,但问题似乎并非来自此特定的 pymongo 版本。

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 !我想知道你们中是否有人找到了从 pymongo 客户端使用此 MongoDB object 类型的解决方案...感谢您的帮助!

You just need to store an instance of datetime.datetime. 您只需要存储一个datetime.datetime实例。

Inserting from the python shell: 从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')}

Querying in the mongo shell: 在mongo shell中查询:

> db.test.findOne()
{
    "_id" : ObjectId("4e8b388367d5bd2de0000000"),
    "date" : ISODate("2011-10-04T16:46:59.786Z")
}

For those who are wondering how to create ISODate from timestamp: 对于那些想知道如何从时间戳创建ISODate的人:

ts = time.time()
isodate = datetime.datetime.fromtimestamp(ts, None)

This will create datetime object with no timezone. 这将创建没有时区的datetime对象。 When inserted to MongoDB it will get converted to proper ISODate() . 当插入MongoDB时,它将转换为适当的ISODate()

Also, I strongly recommend looking at Python TimeTransitionsImage . 另外,我强烈建议您查看Python TimeTransitionsImage Note that tuple here is named tuple (equivalent to struct in C). 请注意,此处的tuple命名为元组 (相当于C中的struct)。 And also note that tuple fields are not the same as in C counterparts, even though the naming is the same (for instance, tm_wday starts with Monday and not Sunday). 还要注意,即使命名相同,元组字段也与C对应的字段不同(例如,tm_wday以星期一而不是星期日开始)。

Actually that does not work either. 实际上,这也不起作用。 When you try to use either utcfromtimestamp or fromtimestamp, the program errors out saying that it needs a float. 当您尝试使用utcfromtimestamp或fromtimestamp时,程序会错误地指出需要浮点数。 Just parse the string into a date time object and use that directly in the Mongodb. 只需将字符串解析为日期时间对象,然后直接在Mongodb中使用即可。 filter 过滤

from_dt = datetime.strptime('2018-04-01','%Y-%m-%d')
#from_dts = datetime.utcfromtimestamp(from_dt)
to_dt = datetime.strptime('2018-04-30','%Y-%m-%d')
#to_dts = datetime.utcfromtimestamp(to_dt)
filterCondition = { 
    "LastLogin" : { "$lte" : to_dt},
    "LastLogin" : { "$gte" : from_dt}
}

And then 接着

db[(colName)].find({ "<colName>" : filterCondition }) 

Would work... 会工作...

result = db.objects.insert_one(
   {"last_modified": datetime.datetime.utcnow()})

Here utc stands for Universal Time Coordinates. utc在这里代表世界时间坐标。

For create a document with specific date, for example 03/10/1999, run this:要创建具有特定日期的文档,例如 03/10/1999,请运行以下命令:

from datetime import datetime
from pymongo import MongoClient

db = MongoClient().db_name

date = datetime(1999, 03, 10)
db.collection.insert_one({'date': date})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM