Uploaded image for project: 'Drivers'
  1. Drivers
  2. DRIVERS-518

Add API-level documentation for restriction of geo commands in count helpers

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Component/s: None

      With the deprecation of the count() and command users will be asked to move to the use of countDocuments() and estimatedDocumentCount() helpers.

      The deprecation messaging in the api and deprecation warning should mention the restrictions on using the following commands/operators in the query/filter of the count and the countDocuments helpers.

      restricted -> replacement:

      $where -> $expr
      $near -> $geoWithin with $center
      $nearSphere -> $geoWithin with $centerSphere

      Also note that $expr requires MongoDB 3.6+ ($geoWithin was new in MongoDB 2.4, replacing $within)

      Some examples:

      $where -> $expr

      >>> c.legacy.where.insert_one({'_id': 1})
      <pymongo.results.InsertOneResult object at 0x7f3bd73f2388>
      >>> c.legacy.where.find_one({'$where': 'function() {return this._id == 1;}'})
      {'_id': 1}
      >>> c.legacy.where.count({'$where': 'function() {return this._id == 1;}'})
      1
      >>> c.legacy.where.count_documents({'$where': 'function() {return this._id == 1;}'})
      Traceback (most recent call last):
      ...
      pymongo.errors.OperationFailure: $where is not allowed in this context
      >>> c.legacy.where.count_documents({'$expr': {'_id': 1}})
      1
      

      $near -> $geoWithin with $center

      >>> c.geo.legacy.insert_one({'loc': [-50, 50]})
      <pymongo.results.InsertOneResult object at 0x7f3bdcbeafc8>
      >>> c.geo.legacy.create_index([('loc', '2d')])
      'loc_2d'
      >>> c.geo.legacy.find_one({'loc': {'$near': [-73, 40]}})
      {'loc': [-50, 50], '_id': ObjectId('5b2ec3b49110ea19b6a54bb4')}
      >>> c.geo.legacy.count({'loc': {'$near': [-73, 40]}})
      1
      >>> c.geo.legacy.count_documents({'loc': {'$near': [-73, 40]}})
      Traceback (most recent call last):
      ...
      pymongo.errors.OperationFailure: $geoNear, $near, and $nearSphere are not allowed in this context
      >>> c.geo.legacy.count_documents({'loc': {'$geoWithin': {'$center': [[-73, 40], 30]}}})
      1
      

      $nearSphere -> $geoWithin with $centerSphere

      >>> c.geo.legacy.find_one({'loc': {'$nearSphere': [-73, 40]}})
      {'loc': [-50, 50], '_id': ObjectId('5b2ec3b49110ea19b6a54bb4')}
      >>> c.geo.legacy.count({'loc': {'$nearSphere': [-73, 40]}})
      1
      >>> c.geo.legacy.count_documents({'loc': {'$nearSphere': [-73, 40]}})
      Traceback (most recent call last):
      ...
      pymongo.errors.OperationFailure: $geoNear, $near, and $nearSphere are not allowed in this context
      >>> c.geo.legacy.count_documents({'loc': {'$geoWithin': {'$centerSphere': [[-73, 40], 10]}}})
      1
      

       

       

       

            Assignee:
            Unassigned Unassigned
            Reporter:
            scott.lhommedieu@mongodb.com Scott L'Hommedieu (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: