先附上mongodb的数据存储结构
然后直接附上完整代码:
- import pandas as pd
- import pymongo
- import time
-
-
- def get_time_stamp(str_time): # 字符串转时间戳, 获取13位时间戳
- time_array = time.strptime(str_time, "%Y-%m-%d %H:%M:%S")
- time_stamp = time.mktime(time_array) * 1000
- return int(time_stamp)
-
-
- def get_time_str(time_stamp): # 时间戳转字符串
- time_array = time.localtime(int(time_stamp) / 1000)
- time_str = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
- return time_str
-
-
- def get_data_by_tum(localID, begin_time, end_time, mongoaddress, mongobase):
- """
- # 从MongoDB中读取数据
- :param localID: list [deviceid,dataoffset,category],[56601,8,20]
- :param begin_time: 数据开始时间,'2023-03-10 15:14:13'
- :param end_time: 数据截止时间,'2023-04-20 15:14:13'
- :param mongoaddress: MongoDB address,'mongodb://username:password@host:port'
- :param mongobase: MongoDB name,'dbname'
- :return:
- """
- client = pymongo.MongoClient(mongoaddress) # 连接mongodb
- db = client.get_database(mongobase) # 获取db库
- collettions = db[str(localID[0])] # 获取collections中某个collection
- result = collettions.find(
- {'dataoffset': str(localID[2]),
- 'category': str(localID[1]),
- 'nowtime': {'$gte': begin_time, '$lte': end_time}}) # 获取该collection下面的数据
- timelist = []
- valuelist = []
- for r in result:
- timelist.append(get_time_str(r.get('nowtime'))) # 时间列表
- valuelist.append(float(r.get('value'))) # 数值列表
- result.close()
- if len(timelist) > 0:
- returndata = pd.DataFrame({'time': timelist, 'value': valuelist}) # 转换成数据框输出
- else:
- returndata = pd.DataFrame()
- return returndata
-
-
- if __name__ == '__main__':
- localID = [65826, 60, 1]
- mongo_full_address = "mongodb://username:password@host1:port1, host2:port2, host3:port3"
- mongobase = "dbname"
- begin_time = get_time_stamp('2023-07-10 15:14:13')
- end_time = get_time_stamp('2023-08-20 15:14:13')
- dataget = get_data_by_tum(localID, begin_time, end_time, mongo_full_address, mongobase)
- print(dataget)