1

I have the following piece of code in python which is trying to pass a mongo query. Details of the connection are below. When I execute this, I get "invalid syntax" and the syntax error points right after the code "total:{"... I think this has something to do with dictionary objects in Python but I am a little confused how to proceed.

import pymongo

from pymongo import MongoClient

client = MongoClient()

db = client.trackers

a = list((db.monthlytracker.aggregate([{$match:   
          {'Country':'Japan','Vendor':'Others'}}, 
          {$group:{_id:"$Fiscal Quarter",total:{$sum:"$Units"}}}])))
0

1 Answer 1

2

You are using the wrong syntax. The pymongo driver accepts only strings:

a = list((db.monthlytracker.aggregate([{'$match':   
          {'Country':'Japan','Vendor':'Others'}}, 
          {'$group':{'_id':"$Fiscal Quarter",'total':{'$sum':"$Units"}}}])))

Simply quote everything, and you'll do fine.

The problem is that python looks to evaluate $group $match and so on, and it does not know about these variables. So you probably get errors like:

$match
  File "<ipython-input-1-972ca9ab5d06>", line 1
    $match
    ^
SyntaxError: invalid syntax

or:

NameError: name 'total' is not defined

and one more final thing, be careful with turning cursors to list objects, as you do with:

list(db.monthly.tracker.aggregate(...))

This can easily consume your memory. Instead you should iterate over the items in the cursor as with:

for item in db.monthly.tracker.aggregate(...):
    do_your_magic_here(item)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Appreciate the quick response. So I tried quoting everything exactly as above but got a new error which said: KeyError: "['Units' 'MS Quarter'] not in index". I think this has more to do with the dictionary object issue. For the original error, I didn't actually get a syntax error on match or the group function, it was when it hit "total" in the code. Thanks for the recommendation on the memory usage.
Correction: I think quoting match and group is necessary. The syntax error actually was pointing under $match to begin with...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.