
您可以使用Python中的pymongo库连接到MongoDB数据库并使用它在Python中插入、更新、删除等对象。该库支持开箱即用的 Python 日期时间对象,您无需执行任何特殊操作即可使用 PyMongo 在 Mongo 中插入日期。例如,
示例
from pymongo import MongoClient
# This will try to connect to MongoDB on the default port and host
client = MongoClient()
db = client.test_database
# Insert the given dictionary to the objects collection:
result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()})
print("Object inserted!")输出
这将给出输出 -
这本书给出了一份关于python这门优美语言的精要的参考。作者通过一个完整而清晰的入门指引将你带入python的乐园,随后在语法、类型和对象、运算符与表达式、控制流函数与函数编程、类及面向对象编程、模块和包、输入输出、执行环境等多方面给出了详尽的讲解。如果你想加入 python的世界,David M beazley的这本书可不要错过哦。 (封面是最新英文版的,中文版貌似只译到第二版)
Object inserted!
注意 - 始终使用 datetime.datetime.utcnow(),它返回 UTC 中的当前时间,而不是 datetime.datetime.now(),它返回当前本地时间。










