导包
import pymongo
import time
from datetime import datetime
获得当前日期
# 方法一:当前日期
current_data = time.strftime("%Y-%m-%d", time.localtime())
print(current_data)
# # 方法二:当前日期
# time = datetime.date(datetime.now())
# print(time)
连接mongodb数据库
# 1建立连接
client = pymongo.MongoClient('127.0.0.1', 27017)
print(client)
# 2新建名为weather的数据库
dataName = client['weather']
# 3新建名为tb_2021_05_24的表
table_name = "tb_{}".format(current_data)
tb = dataName[table_name]
写入数据
image_name = '31011529001320014350-2021-05-14-10-53-37.976.jpeg'
image_reuslt = [{'aitype': 'litter', 'cameraID': '31011529001320014350', 'nodeName': '58', 'processdate': '2021-05-14 10:53:37', 'ptype': 'localfile', 'recorddate': '2021-05-14 10:53:37', 'result': {'cls': [1], 'h': [81], 'message': 'ok', 'pro': [0.862], 'w': [63], 'x': [945], 'y': [720]}, 'url': 'D:/Project/spider_picture/img3/31011529001320014350-2021-05-14-10-53-37.976.jpeg'}]
# 写入数据
tb.insert_one({'image_name': image_name, 'image_result': image_reuslt[0]})
one = tb.find_one()
print(one)
查询数据
def findByRegex(cameraID):
c_id = "^{}.*".format(cameraID)
results = mycol.find({'image_name': {'$regex': c_id}}).sort('_id', pymongo.DESCENDING).limit(1)
return results[0]
def findById(cameraID):
results = mycol.find({'cameraID': cameraID}).sort('_id', pymongo.DESCENDING).limit(1)
return results[0]
ax = findByRegex('31011529001320014350')
print(ax)