如何准备一个已插入 MongoDB 的 Python 日期对象?
你可以使用 Python 中的 pymongo 库连接到 MongoDB 数据库并使用它来插入、更新、删除等 Python 中的对象。该库支持 Python datetime 对象,而无需执行任何特殊操作即可使用 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(),它会返回当前的本地时间。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP