Python 类型错误:';命令光标';对象没有属性'__获取项目';
当我试图通过Apache服务器访问瓶子的RESTAPI时,我遇到了这个类型错误,但是它与瓶子的WSGI服务器一起正常工作 Mongodb示例数据:Python 类型错误:';命令光标';对象没有属性'__获取项目';,python,apache,mongodb,pymongo,bottle,Python,Apache,Mongodb,Pymongo,Bottle,当我试图通过Apache服务器访问瓶子的RESTAPI时,我遇到了这个类型错误,但是它与瓶子的WSGI服务器一起正常工作 Mongodb示例数据: "_id" : ObjectId("55c4f21782f2811a08b7ddbb"), "TestName" : "TestName1", "Results" : [ { "Test" : "abc", "Log" : "Log information" },
"_id" : ObjectId("55c4f21782f2811a08b7ddbb"),
"TestName" : "TestName1",
"Results" : [
{
"Test" : "abc",
"Log" : "Log information"
},
{
"Test" : "xyz",
"Log" : "Log information"
},
]
我只想获取那些显示结果的记录/子文档。Test=“abc”
我的瓶子API代码:
@route('/TestSampleApi',method='GET')
def GetTestData():
#request.json will have data passed in url
pipe = [
{"$match":{"Results": {"$elemMatch": {'Test':'abc'}}}},
{ "$unwind" :"$Results"},
{ "$match": { "Results.Test":'abc'}},
{ "$group" : { "_id" : "$_id", "Results" : { "$addToSet" : "$Results" } }}
]
res = collection.aggregate(pipeline=pipe)
get_record=res['result'] #getting TypeError in this line
response.set_header('Content-Type', 'application/json')
return dumps(get_record, indent=4)
上面的代码可以正常使用
curl-我明白了”http://localhost:8080/TestSampleApi?Test=abc“
但不使用Apache:
curl-我明白了”http://HOST_NAME/TestSampleApi?Test=abc“
在PyMongo 3中,聚合方法返回结果文档的一个iterable(CommandCursor的实例),而不是单个文档。您必须迭代结果,或者将结果转换为一个带有列表(res)的列表。谢谢这一点-我一直想知道切换到新机器后出现了什么问题。结果
字典现在是抽象的。OP可以使用get\u record=res.next()
或对列表进行类型转换。