**
- I am getting cursor object and not get the data from db. Here is the code with output I need help on that and I alos post the output;
** 当我试图获得输出时,它将给出游标对象,我试图从数据库中获得数据,并将完整的代码导出为csv到该代码中,我使用了pymongo的聚合功能。
import pymongo
from pymongo import MongoClient
import json
import time
from pymongo.errors import ConnectionFailure
starttime = time.time()
import pandas as pd
from datetime import datetime
today = datetime.strftime(datetime.now(),"%Y-%m-%d")
# print(today)
#connect to database
client = MongoClient()
x= []
docs = pd.DataFrame(columns=[])
with client:
db = client['local']
col= db.daily_dim_appname_cdn
Total_Bandwidth = float(uncache) + float(cache)
Actual_Internet_Bandwidth == uncache
Peak_Bandwidth = uncache + cache
cursor = list(col.find({},{"recorddate":1,"cdn":1,"tp":1}))
# print(cursor)
for i in cursor:
if i['cdn'] == 'Internet':
uncache = col.aggregate([
{'$project':{'year': { '$year': "$recorddate" },'month': {' $month': "$recorddate" },"day":{"$dayOfMonth":"$recorddate" },'cdn':1,'tp':1,'recorddate':1}},
{'$match':{"year" :2020,'month':1,'day':10,'tp':{'$gt':0},'cdn':{'$in':['Internet']}}},
{'$group' : {'_id': '$recorddate', 'sumtp': { '$sum' : '$tp' }}},
{'$project':{'_id':0,'rdate':'$_id','sumtp':{'$divide':['$sumtp',1000000000]},'rmonth': { '$month': "$_id" }}},
])
print("uncache",uncache)
elif i['cdn'] != 'Internet':
cache= col.aggregate([
{'$project':{'year': { '$year': "$recorddate" },'month': {' $month': "$recorddate" },'cdn':1,'tp':1,'recorddate':1}},
{'$match':{"year" :2020,'month':1,'tp':{'$gt':0},'cdn':{'$nin':['Internet']}}},
{'$group' : {'_id': '$recorddate', 'sumtp': { '$sum' : '$tp' }}},
{'$project':{'_id':0,'rdate':'$_id','sumtp':{'$divide':['$sumtp',1000000000]},'rmonth': { '$month': "$_id" }}},
])
print("cache",cache)
output:
cache <pymongo.command_cursor.CommandCursor object at 0x113180278>
cache <pymongo.command_cursor.CommandCursor object at 0x1131802b0>
uncache <pymongo.command_cursor.CommandCursor object at 0x113180438>
uncache <pymongo.command_cursor.CommandCursor object at 0x1131802e8>
uncache <pymongo.command_cursor.CommandCursor object at 0x113180358>
uncache <pymongo.command_cursor.CommandCursor object at 0x1131804e0>
uncache <pymongo.command_cursor.CommandCursor object at 0x113180278>
uncache <pymongo.command_cursor.CommandCursor object at 0x113180208>
uncache <pymongo.command_cursor.CommandCursor object at 0x113180240>
uncache <pymongo.command_cursor.CommandCursor object at 0x113180438>
cache <pymongo.command_cursor.CommandCursor object at 0x113180470>
uncache <pymongo.command_cursor.CommandCursor object at 0x1131802e8>
cache <pymongo.command_cursor.CommandCursor object at 0x113180358>
cache <pymongo.command_cursor.CommandCursor object at 0x1131804e0>
uncache <pymongo.command_cursor.CommandCursor object at 0x113180278>
enter code here
请尝试迭代缓存和取消缓存。像。
for i in cache:
print(i)
break
我希望你会得到数据从 i['key']
.