如何准备要插入MongoDB的Python日期对象?
您可以使用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("对象已插入!")输出结果
这将给出输出-
对象已插入!
注意-始终使用datetime.datetime.utcnow()(以UTC返回当前时间),而不要使用datetime.datetime.now()(以返回当前本地时间)。
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短