![](/img/trans.png)
[英]TypeError(“'NoneType' object has no attribute '__getitem__'”,)
[英]TypeError: 'CommandCursor' object has no attribute '__getitem__'
当我试图通过Apache服务器访问Bottle的rest API时,我得到了这个TypeError,但它与Bottle的WSGI服务器一起正常工作。
Mongodb样本数据:
"_id" : ObjectId("55c4f21782f2811a08b7ddbb"),
"TestName" : "TestName1",
"Results" : [
{
"Test" : "abc",
"Log" : "Log information"
},
{
"Test" : "xyz",
"Log" : "Log information"
},
]
我想只获取那些记录/子文档,其中Results.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 -i GET "http://localhost:8080/TestSampleApi?Test=abc"
但不使用Apache: curl -i GET "http://HOST_NAME/TestSampleApi?Test=abc"
在PyMongo 3中,aggregate方法返回一个可迭代的结果文档(CommandCursor的一个实例),而不是单个文档。 您必须迭代结果,或者将它们转换为带有列表(res)的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.