-
Type: Task
-
Resolution: Fixed
-
Priority: 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
- depends on
-
RUST-61 Add API-level documentation for restriction of geo commands in count helpers
- Closed
-
CSHARP-2310 Add API-level documentation for restriction of geo commands in count helpers
- Closed
-
CXX-1604 Add API-level documentation for restriction of geo commands in count helpers
- Closed
-
JAVA-2893 Add API-level documentation for restriction of geo commands in count helpers
- Closed
-
MOTOR-247 Add API-level documentation for restriction of geo commands in count helpers
- Closed
-
NODE-1529 Add API-level documentation for restriction of geo commands in count helpers
- Closed
-
PHPLIB-359 Document alternatives to restricted query operators for countDocuments()
- Closed
-
PYTHON-1595 Add API-level documentation for restriction of geo commands in count helpers
- Closed
-
RUBY-1370 Add API-level documentation for restriction of geo commands in count helpers
- Closed
-
GODRIVER-478 Add API-level documentation for restriction of geo commands in count helpers
- Closed
-
CDRIVER-2721 Add API-level documentation for restriction of geo commands in count helpers
- Closed