python - Get result from mongo aggregation using pymongo 3.0 -
i know may simple question cant figure out, im getting same error again , again:
i amb using python query mongo collection , retrive value it, here is:
subquery = db.partsupp.aggregate([ {"$match": {"r_name": region }}, {"$group": { "_id" : 0, "minim": {"$min": "$supplycost"} } } ])
this query works fine , outputs:
[{'_id': 0, 'minim': 10}]
what trying 'minim' value aggregation.
initialy wanted 'if' check if query had result, this:
if len(subselect['result']) > 0 : minim = subquery['result'][0]['minim'] else: return subselect
but doing following error:
traceback (most recent call last): file "query2.py", line 195, in <module> pprint( list(query2('catalonia', 1, 1)) ) file "query2.py", line 72, in query2 if len(subquery['result']) > 0 : typeerror: 'commandcursor' object not subscriptable
looks result 'subselect' query not iterable or that, youo have idea on how solve ?
i using python 3.4.3 , pymongo 3.0.1
thanks in advance
pymongo 3.0.1 returns aggregation results cursor, mean can't access result subquery['result']. disable cursor , force pymongo return document {'result':{...}} instead of cursor, use this:
subquery = db.partsupp.aggregate([ {"$match": {"r_name": region }}, {"$group": { "_id" : 0, "minim": {"$min": "$supplycost"} } } ], usecursor=false)
Comments
Post a Comment