如何准备一个要插入到 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