Array in Python and Bottle

64 views
Skip to first unread message

Totti

unread,
Sep 24, 2015, 6:12:42 PM9/24/15
to mongodb-user
I am trying to pass the results of differents aggregates in an array. But have a cursor error which I cannot figure out. when aggregations are replaced by string values, it works. Could anyone figure out where I am doing it wrong? any help would be appreciated. here the codes.

---------This is the error-----------------------------------------
  • <pymongo.command_cursor.CommandCursor object at 0x0000000002B122B0>
  • <pymongo.command_cursor.CommandCursor object at 0x0000000002B492B0>


--------analytics.py-------------------------------------

#ALL AGGREGATES
@bottle.route('/all/')
def home_page():
    # connect to mongoDB
    connection = pymongo.MongoClient('localhost', 27017)

    # attach to test database
   
    db = connection.customers
    # get handle for names collection
    complaints = db.complaints
   
    # find a single document
    aggr1 = db.complaints.aggregate([{"$group":{"_id":"$disputed", "Total_Disputed":{"$sum":1}}},{"$sort":{"Total_Disputed":-1}},{"$project":{"_id":0, "disputed":"$_id", "Total_Disputed":1}}])
    aggr2 = db.complaints.aggregate([{"$group":{"_id":"$product", "Total_Product":{"$sum":1}}},{"$sort":{"Total_Product":-1}},{"$project":{"_id":0, "product":"$_id", "Total_Product":1}}])
    aggr = [aggr1,aggr2]
   
    return bottle.template('analytics', {'username':"Dispute Aggregate Analytics", 'things':aggr})



bottle.debug(True)
bottle.run(host='localhost', port=8082)

----------------analytics.tpl----------------------------------------------------------------------------------------
<!DOCTYPE hml>
<html>
<head>
<title> @2015 </title>
</head>
<body>
<p>
 {{username}}
</p>
<p>
<ul>
%for thing in things:
<li>{{thing}}</li>
%end
</ul></p>

</body>
</html>


A. Jesse Jiryu Davis

unread,
Sep 27, 2015, 8:47:39 PM9/27/15
to mongodb-user
aggr1 and aggr2 are cursors. When you call aggregate() it gets the first 100 results of the aggregation from MongoDB and returns a cursor to you. To get all the results of one aggregation, do:

results1 = list(aggr1)

If you want to combine the two lists, you could do:

results = list(aggr1).extend(list(aggr2))
Reply all
Reply to author
Forward
0 new messages