mongo数据入库时间格式设置

        在 MongoDB 中,可以使用 BSON(Binary JSON)格式存储日期和时间数据。BSON 是一种二进制编码格式,可以在 MongoDB 中高效地存储和检索数据。

        要将日期和时间数据存储为 BSON 格式,可以使用 Python 中的 datetime 模块来生成 datetime 对象,并将其传递给 bson.datetime.datetime() 方法。例如:

import bson
from datetime import datetime

# 创建一个 datetime 对象
now = datetime.now()
print(now)

# 将 datetime 对象转换为 BSON 格式
bson_datetime = bson.datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)
print(bson_datetime)
print(type(bson_datetime))

运行结果:

写入mongo测试:

from pymongo import MongoClient
import bson
from datetime import datetime

my_client = MongoClient('mongodb://root:88888888@192.168.124.49:27017')
my_db = my_client['test-mongo']
# print(my_db)
my_col = my_db['test_time']
# print(my_col)

# 创建一个 datetime 对象
now = datetime.now()
print(now)

# 将 datetime 对象转换为 BSON 格式
bson_datetime = bson.datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)
print(bson_datetime)

dic = {'time': bson_datetime}
data = my_col.insert_one(dic)
print(data)

效果展示:

        如果您希望以特定的日期和时间格式存储数据,可以在创建 datetime 对象时使用 strftime() 方法指定格式化字符串。例如:

import bson
from datetime import datetime

# 创建一个 datetime 对象
now = datetime.now()
print(now)

# 将 datetime 对象转换为 BSON 格式, 并使用特定的日期和时间格式
bson_datetime = bson.datetime.datetime.strptime(now.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
print(bson_datetime)

运行 结果: 

        在上述代码中,strftime() 方法用于将 datetime 对象转换为指定的日期和时间格式(例如:'%Y-%m-%d %H:%M:%S'),然后在 strptime() 方法中将其解析为 datetime 对象。

写入mongo测试:

from pymongo import MongoClient
import bson
from datetime import datetime

my_client = MongoClient('mongodb://root:88888888@192.168.124.49:27017')
my_db = my_client['test-mongo']
# print(my_db)
my_col = my_db['test_time']
# print(my_col)

# 创建一个 datetime 对象
now = datetime.now()
print(now)

# 将 datetime 对象转换为 BSON 格式, 并使用特定的日期和时间格式
bson_datetime = bson.datetime.datetime.strptime(now.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
print(bson_datetime)

dic = {'time': bson_datetime}
data = my_col.insert_one(dic)
print(data)

效果展示:

        需要注意的是,在 MongoDB 中,日期和时间数据存储为 UTC 时区,因此在将数据存储到数据库中之前,应该将其转换为 UTC 时区。您可以使用 pytz 模块来实现时区转换。例如:

import bson
import pytz
from datetime import datetime

# 创建一个 datetime 对象
now = datetime.now()
print(now)

# 将本地时间转换为 UTC 时间
local_timezone = pytz.timezone('Asia/Shanghai')  # 设置本地时区
local_datetime = local_timezone.localize(now)  # 确定本地时间的时区
utc_timezone = pytz.timezone('UTC')  # 设置目标时区为 UTC
utc_datetime = local_datetime.astimezone(utc_timezone)  # 将本地时间转换为 UTC 时间
print(local_timezone)
print(local_datetime)
print(utc_timezone)
print(utc_datetime)

# 将 UTC datetime 对象转换为 BSON 格式,并存储到 MongoDB 中
bson_datetime = bson.datetime.datetime(utc_datetime.year, utc_datetime.month, utc_datetime.day, utc_datetime.hour, utc_datetime.minute, utc_datetime.second)
print(bson_datetime)

运行结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值