Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-403

Don't use $query when querying against the $cmd collection.

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.4
    • Affects Version/s: 2.3
    • Component/s: None
    • None

      This is related to PYTHON-341 and PYTHON-367. A query is wrapped in $query whenever a cursor option is used ($hint, $maxScan, $explain, $sort, etc.). This worked fine in PyMongo 2.2 since these options are only used in normal queries. PYTHON-367 added a $readPreference, which is added for certain commands when connected to a mongos. This isn't a problem in MongoDB 2.2, which expects $query in this case. However, it breaks in some queries against $cmd in older versions:

      >>> db.create_collection('foobar', max=False, capped=False, size=0)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "pymongo/database.py", line 245, in create_collection
          return Collection(self, name, **opts)
        File "pymongo/collection.py", line 108, in __init__
          self.__create(kwargs)
        File "pymongo/collection.py", line 119, in __create
          self.__database.command("create", self.__name, **options)
        File "pymongo/database.py", line 388, in command
          result = self["$cmd"].find_one(command, **extra_opts)
        File "pymongo/collection.py", line 516, in find_one
          for result in self.find(spec_or_id, *args, **kwargs).limit(-1):
        File "pymongo/cursor.py", line 778, in next
          if len(self.__data) or self._refresh():
        File "pymongo/cursor.py", line 729, in _refresh
          self.__uuid_subtype))
        File "pymongo/cursor.py", line 686, in __send_message
          self.__uuid_subtype)
        File "pymongo/helpers.py", line 104, in _unpack_response
          error_object["$err"])
      pymongo.errors.OperationFailure: database error: unrecognized command: $query
      
      Sun Sep  9 14:48:58 ./mongos db version v2.0.6, pdfile version 4.5 starting (--help for usage)
      ...
      Sun Sep  9 14:49:59 [conn2] Request::process ns: foo.$cmd msg id:2019554530 attempt: 0
      Sun Sep  9 14:49:59 [conn2] single query: foo.$cmd  { $query: { create: "foobar", max: false, capped: false, size: 0.0 }, $readPreference: { mode: "primary" } }  ntoreturn: -1 options : 0
      Sun Sep  9 14:49:59 [conn2] User Assertion: 13390:unrecognized command: $query
      

      This only seems to affect certain commands like "$eval" and "create".

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            bernie@mongodb.com Bernie Hackett
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: