我想插入我的查询对象在pymongo连接器创建大熊猫数据帧:格式MongoDB的查询中使用Pymongo
import pandas as pd
from pymongo import MongoClient
def _connect_mongo(host, port, username, password, db):
if username and password:
mongo_uri = 'mongodb://%s:%[email protected]%s:%s/%s' % (username, password, host, port, db)
conn = MongoClient(mongo_uri)
else:
conn = MongoClient(host, port)
return conn[db]
def read_mongo(db, collection, query={}, host='localhost', port=27017, username=None, password=None, no_id=True):
""" Read from Mongo and Store into DataFrame """
# Connect to MongoDB
db = _connect_mongo(host=host, port=port, username=username, password=password, db=db)
# Make a query to the specific DB and Collection
cursor = db[collection].find(query)
# Expand the cursor and construct the DataFrame
df = pd.DataFrame(list(cursor))
# Delete the _id
# if no_id:
# del df['_id']
return df
我的查询被定义为:
query_1 = "{
"status" : {"$ne" : "deprecated"},
"geoLocationData.date" : { $gte : new ISODate("2016-08-03") }
},
{ "geoLocationData.date": 1,
"geoLocationData.iso": 1,
"httpRequestData.ipAddress": 1,
"geoLocationData.city": 1,
"geoLocationData.latitude": 1,
"geoLocationData.longitude": 1 }"
将其插入 - 获得一个数据帧大熊猫:
df = read_mongo(db, collection, query_1, host, port, username, password)
我得到的错误:
TypeError: filter must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping
如果我只是省略子文档,查询工作得很好,我可以将其转换为数据框。
我想这是关于将我的查询转换成字典(与子文件)。 我该怎么做?
你是否按照'query_1 =“db.finger ......})”'的含义给查询提供了一个字符串? –
对不起,我编辑过。我正在定义查询省略查找语句@SteveRossiter – xxxvinxxx