1.创建时间字段
#1.创建时间字符串字段 下面2个等价
item['create_time'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 2022-05-11 00:00.00
item['create_time'] = (datetime.utcnow() + timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S") # 2022-05-11 00:00.00
#2.更新到数据库 怎么链接数据库这我就不写了
self.col.update({"_id": item['_id'] }, {'$set':dict(item)}, True)
2.查询时间段的数据
new = '2022-05-11 '
myDatetime1 = new + '00:00.00'
myDatetime2 = new + '23:59:59'
print(self.file, myDatetime1, myDatetime2)
#2.构建查询条件
myquer = {
'create_time': {'$gte': myDatetime1, '$lte': myDatetime2},
}
#3.进行查询
cursor = self.col.find(myquer, batch_size=5)
for data in cursor[:]:
print(data) # 每条数据