您可以使用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!")
登录后复制
输出
这将给出输出 –
Object inserted!
登录后复制
注意 – 始终使用 datetime.datetime.utcnow(),它返回 UTC 中的当前时间,而不是 datetime.datetime.now(),它返回当前本地时间。
以上就是如何在 Mongodb 中插入 Python 对象?的详细内容,更多请关注www.92cms.cn其它相关文章!






