使用 pyMongo 创建 ISODate

发布于 2024-12-08 12:53:22 字数 288 浏览 0 评论 0原文

我一直在尝试找到一种方法来使用 pyMongo 客户端创建 ISODate 对象,但到目前为止没有任何成功。

我使用 http://pypi.python.org/pypi/pymongo3 客户端,这是 Python 中唯一可用的严肃客户端目前是 3.3,但问题似乎不是来自这个特定的 pymongo 版本。

我想知道你们中是否有人找到了从 pymongo 客户端使用此 MongoDB 对象类型的解决方案...感谢您的帮助!

I've been trying to find a way to create an ISODate object whith pyMongo client, but without any success so far.

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.

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凉墨 2024-12-15 12:53:22

您只需要存储 datetime.datetime 的实例。

从 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 中查询:

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

You just need to store an instance of datetime.datetime.

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')}

Querying in the mongo shell:

> db.test.findOne()
{
    "_id" : ObjectId("4e8b388367d5bd2de0000000"),
    "date" : ISODate("2011-10-04T16:46:59.786Z")
}
若沐 2024-12-15 12:53:22

对于那些想知道如何从时间戳创建 ISODate 的人:

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

这将创建没有时区的 datetime 对象。当插入到 MongoDB 时,它将转换为正确的 ISODate()

另外,我强烈建议查看 Python TimeTransitionsImage。请注意,这里的tuple命名元组(相当于C中的struct)。另请注意,元组字段与 C 对应项中的元组字段不同,即使命名相同(例如,tm_wday 从星期一开始,而不是星期日)。

For those who are wondering how to create ISODate from timestamp:

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

This will create datetime object with no timezone. When inserted to MongoDB it will get converted to proper ISODate().

Also, I strongly recommend looking at Python TimeTransitionsImage. Note that tuple here is named tuple (equivalent to struct in C). 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).

花落人断肠 2024-12-15 12:53:22

事实上这也行不通。当您尝试使用 utcfromtimestamp 或 fromtimestamp 时,程序会出错,指出它需要一个浮点数。只需将字符串解析为日期时间对象并直接在 Mongodb 中使用即可。过滤

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}
}

然后

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

就可以了...

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. Just parse the string into a date time object and use that directly in the 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...

沦落红尘 2024-12-15 12:53:22
result = db.objects.insert_one(
   {"last_modified": datetime.datetime.utcnow()})

这里 utc 代表世界时间坐标。

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

Here utc stands for Universal Time Coordinates.

ぃ双果 2024-12-15 12:53:22

要创建具有特定日期的文档,例如 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})

For create a document with specific date, for example 03/10/1999, run this:

from datetime import datetime
from pymongo import MongoClient

db = MongoClient().db_name

date = datetime(1999, 03, 10)
db.collection.insert_one({'date': date})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文