![](/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.