From 912bbc1a4a48271aa1bb249de94573dceafb4857 Mon Sep 17 00:00:00 2001 From: Eran Sandler Date: Tue, 14 Jul 2015 08:19:25 +0300 Subject: [PATCH] Added backwards compatibility mode with older versions of PyMongo. It appears that older versions would return a dictionary from an aggregate operation that had the cursor inside the "result" key. Newer versions return a new type of cursor called CommandCursor. --- redash/query_runner/mongodb.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/redash/query_runner/mongodb.py b/redash/query_runner/mongodb.py index dcfb4c3151..2ab6136fd6 100644 --- a/redash/query_runner/mongodb.py +++ b/redash/query_runner/mongodb.py @@ -244,7 +244,16 @@ def run_query(self, query): elif aggregate: r = db[collection].aggregate(aggregate) - cursor = r["result"] + + # Backwards compatibility with older pymongo versions. + # + # Older pymongo version would return a dictionary from an aggregate command. + # The dict would contain a "result" key which would hold the cursor. + # Newer ones return pymongo.command_cursor.CommandCursor. + if isinstance(r, dict): + cursor = r["result"] + else: + cursor = r for r in cursor: for k in r: