[SERVER-59674] estimatedDocumentCount does not work on views on 5.0+ Created: 30/Aug/21  Updated: 18/Feb/22  Resolved: 18/Feb/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 5.0.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Shane Harvey Assignee: James Wahlin
Resolution: Duplicate Votes: 0
Labels: query-director-triage, read-only-views
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by PYTHON-2885 Namespace xyz is a view, not a collec... Closed
Duplicate
duplicates SERVER-63850 Add the count command to API version 1 Closed
Problem/Incident
causes PYTHON-2885 Namespace xyz is a view, not a collec... Closed
Related
related to DRIVERS-2184 estimatedDocumentCount should use cou... Closed
is related to PYTHON-2853 Document performance implications of ... Backlog
Sprint: QO 2021-09-06
Participants:

 Description   

Due to the changes in DRIVERS-1437, estimatedDocumentCount no longer works on views on 5.0+. For example:

>>> db = client.test
>>> db.view.drop()
>>> db.create_collection('view', viewOn='coll')
>>> db.view.count_documents({})
0
>>> db.view.estimated_document_count()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 1495, in estimated_document_count
    return self.__database.client._retryable_read(
  File "/Users/shane/git/mongo-python-driver/pymongo/mongo_client.py", line 1384, in _retryable_read
    return func(session, server, sock_info, slave_ok)
  File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 1484, in _cmd
    result = self._aggregate_one_result(
  File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 1436, in _aggregate_one_result
    result = self._command(
  File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 221, in _command
    return sock_info.command(
  File "/Users/shane/git/mongo-python-driver/pymongo/pool.py", line 718, in command
    return command(self, dbname, spec, slave_ok,
  File "/Users/shane/git/mongo-python-driver/pymongo/network.py", line 158, in command
    helpers._check_command_response(
  File "/Users/shane/git/mongo-python-driver/pymongo/helpers.py", line 167, in _check_command_response
    raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: PlanExecutor error during aggregation :: caused by :: Namespace test.view is a view, not a collection, full error: {'ok': 0.0, 'errmsg': 'PlanExecutor error during aggregation :: caused by :: Namespace test.view is a view, not a collection', 'code': 166, 'codeName': 'CommandNotSupportedOnView', '$clusterTime': {'clusterTime': Timestamp(1630345764, 1), 'signature': {'hash': b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'keyId': 0}}, 'operationTime': Timestamp(1630345764, 1)}



 Comments   
Comment by James Wahlin [ 18/Feb/22 ]

This ticket is being closed as a duplicate of SERVER-63850 which will add the count command to the stable API for MongoDB 6.0.

It is worth noting that the estimatedDocumentCount helper is a drivers-side function that for MongoDB 4.4 and earlier used the count command to obtain results. This behavior changed for MongoDB 5.0 because of a change to use {$collStats: {$count: 1}} to obtain this count, a mechanism that does not fall back to exact count for views and instead returns an error. This change was made due to a decision not to add the count command to the stable API in the server.

For MongoDB 6.0 we are planning to restore use of the count command in drivers and will add it to the stable API.

For users impacted by this change in MongoDB 5.0, the workaround is to make use of the count command directly rather than use the estimatedDocumentCount helper. Please note that use of the count command must be done via Client object that does not specify API strict.

Comment by Emily Pakulski (Inactive) [ 18/Jan/22 ]

Just wanted to raise that InTel would still really appreciate an option for views and time-series collections. We have had a string of follow-up tickets related to this. A key issue is that the agent (which runs the $count operation) doesn't readily have available metadata about what kind of collection it's running the $count operation on, so we are ending up with a bespoke set of fallback logic. Fixing this would clean things up for us.

Comment by Luiz Otavio Duarte | Facti [ 03/Nov/21 ]

I suggest you use the "count_documents" call instead... until an effective solution is available.

Comment by Julia Ruddy (Inactive) [ 25/Oct/21 ]

Another FYI-- Data Explorer relies on estimatedDocumentCount to populate a few of our statistics in the UI. This is causing the aggregation pipeline builder to error out for views and time-series collections. 

Generated at Thu Feb 08 05:47:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.