2015-08-12 26 views
8

sto ottenendo questo TypeError quando si cerca di accedere alla API riposo di bottiglia attraverso Apache server, ma sta funzionando correttamente con il server WSGI di Bottiglia.TypeError: oggetto 'CommandCursor' non ha alcun attributo '__getitem__'

MongoDB dati di esempio:

"_id" : ObjectId("55c4f21782f2811a08b7ddbb"), 
"TestName" : "TestName1", 
"Results" : [ 
    { 
      "Test" : "abc", 
      "Log" : "Log information" 
    }, 
    { 
      "Test" : "xyz", 
      "Log" : "Log information" 
    }, 
] 

voglio prendere solo i record/sub documento in cui Results.Test = "abc"

Il mio codice Bottiglia 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) 

Sopra il codice funziona correttamente con curl -i GET "http://localhost:8080/TestSampleApi?Test=abc"
Ma non funziona con Apache: curl -i GET "http://HOST_NAME/TestSampleApi?Test=abc"

risposta

10

In PyMongo 3 il metodo di aggregazione restituisce un iterable di documenti risultato (un'istanza di CommandCursor), non un singolo documento. Devi iterare i risultati, o alternativamente trasformarli in una lista con lista (res).

+1

Grazie per questo - ho continuato a chiedermi cosa sarebbe andato storto dopo essere passato a una macchina nuova. Il dizionario 'result' è ora astratto. OP potrebbe usare 'get_record = res.next()' o typecast in una lista. – Spade

Problemi correlati