简体   繁体   中英

Why am I getting invalid error in my PyMongo Query

I am writing a pymongo query and it works when I write it in my MongoDB GUI but keep getting an SyntaxError: invalid syntax error.

What am I doing wrong?

def get_codes(request):
   get_code = newsCode.aggregate([{
    '$match': {
        'site': { 
            '$exists': true 
        },
        'segment': {
            '$exists': true
        }
   }, {
    '$group': {
        '_id': {
            'site': "$site",
            'seg_code': {
                '$substr': ["$segment", 0, 4]
            },
            'segment': "$segment"
        }
    }
   }, {
    '$project': {
        'site': "$_id.site",
        'seg_code': "$_id.seg_code",
        'segment': "$_id.segment"
    }
   }, {
    '$sort': {
        '_id': 1
    }
   }
  }])

My error is showing at the , near

 }, {
  '$project

Traceback:

Traceback (most recent call last):
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check(display_num_errors=True)
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/urls/resolvers.py", line 540, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in urlconf_module
return import_module(self.urlconf_name)
File "/Users/userName/anaconda3/envs/env_dp_36/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/Users/userName/Desktop/Dash/dash/dash/urls.py", line 3, in <module>
from . import views
File "/Users/userName/Desktop/Dash/dash/dash/views.py", line 5, in <module>
from .datamanager import *
File "/Users/userName/Desktop/Dash/dash/dash/datamanager.py", line 471
}, {
 ^
SyntaxError: invalid syntax

Why am I getting this error? Like i said it is working when I do this in my GUI. Is there something wrong with my syntax? Is the PyMongo code supposed to be different from the MongoDB code?

Please help!

Welcome to StackOverflow!

There are a couple of things going on here:

1) When using pymongo, you're writing in python so true has to be changed to True

2) When you try to aggregate these queries together, the aggregate function accepts a list of dictionaries representing a portion of the query. The snippet you posted passes a list into the function where the first entry is the portion containing your $match and $group keys, but the $project key takes place after that dictionary has been closed out by the curly brackets on the line above. I'm not sure why this isn't throwing up an error in the pymongo shell, because there's definitely a mismatch of brackets that mongo wouldn't be happy with either.

I cleaned up the alignment of brackets in the following snippet, though I'm not sure if there might be other issues with the query when it's run in the context of the application.

def get_codes(request):
    get_code = newsCode.aggregate([
        {'$match': {'site': {'$exists': true}, 'segment': {'$exists': true}}},
        {'$group': {
            '_id': {
                'site': "$site",
                'seg_code': {'$substr': ["$segment", 0, 4]},
                'segment': "$segment"
            },
        }},
        {'$project': {
            'site': "$_id.site",
            'seg_code': "$_id.seg_code",
            'segment': "$_id.segment"
        }},
        {'$sort': {'_id': 1}},
    ])

One final note: sorting takes place differently in pymongo than it does when writing a raw mongo query. Ref: How to sort mongodb with pymongo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM