What the agent does
The agent starts from the calls, not the traceback. In Nth-iteration-labs/streamingbandit, a Tornado service for running bandit experiments, the user store in app/db/users.py checks for an existing account with self.userinfo.find({'username':username}).count() > 0 on line 30, inside create_user, before it inserts with insert_one. The bootstrap script insert_admin.py does the same on line 24 to decide whether an admin already exists. Both are Cursor.count: a query built with find and then counted. The rest of the database layer already uses the newer names, insert_one, find_one and count_documents appear in the mocks under tests/, so these two lines are the leftovers.
Three files pin the dependencies three ways: requirements.txt lists pymongo with no version, setup.py and pyproject.toml list it the same way beside redis==2.10.6, and the Dockerfile builds on python:3.5-jessie. A fresh install on Python 3.11 resolves PyMongo 4.19.0, Tornado 6.6 and redis 8.1.0. Against a running MongoDB, the first create_user call then raises the error in the title at line 30; on PyMongo 3.13.0 the same line works and prints DeprecationWarning: count is deprecated. Use Collection.count_documents instead. three times, once per call. The boundary is PyMongo 4.0, published 2021-11-29: 4.0.2 has no Cursor.count, while Collection.count_documents is present in both.
The replacement is one call per site: self.userinfo.count_documents({'username': username}) > 0, and the same in insert_admin.py. The agent does not take the mocked tests as proof, because tests/conftest.py builds the Mongo client as a MagicMock whose find returns a list, so the old line never fails there and the new line never runs. Instead it starts a MongoDB in the sandbox, drops the userinfo database, and drives Users.create_user directly: creating alice returns user id 1, bob returns 2, creating alice again returns False, and the collection then holds exactly those two documents. The same script on PyMongo 3.13.0 with the original line gives the same three answers, which is the equivalence the pull request rests on.
It also names what the same release family removed that this repository does not use, since a reviewer searching for .count() wants the full list: on PyMongo 4, calling insert, update, remove, save, count or find_and_modify on a collection raises TypeError: 'Collection' object is not callable, with a message naming the method, and Cursor.count is gone as here. A search across the repository for those names finds none in this code base beyond the two lines above, and the pull request says so.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "After a dependency refresh, our service fails with \"AttributeError: 'Cursor' object has no attribute 'count'\". Find every call to count() on a find() cursor and every use of the removed Collection methods (insert, update, remove, save, count, find_and_modify, ensure_index) in the project, with file and line. Read every dependency file to say how pymongo is pinned and which release a fresh install resolves, and find by installing releases the first pymongo release without Cursor.count and the last one with it. Replace each cursor count with count_documents on the collection, with the same filter, and each removed method with its named replacement. Do not trust mocked database tests for this: start a MongoDB in the sandbox and drive the changed functions against it, showing that the results match what the old code returns on the last pymongo release that still has count. Run the project's own tests too. Open a pull request with the diff, the version findings and both sets of output.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
- Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
- Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.
What comes back
A pull request that changes line 30 of app/db/users.py and line 24 of insert_admin.py, each find(...).count() becoming count_documents(...) with the filter unchanged. The description names the locked and resolved PyMongo releases, the 4.0 boundary with its date, and the deprecation warning text the old line prints on 3.13.0. Below that is the proof: the script that creates two users and refuses a duplicate, run against a MongoDB in the sandbox on PyMongo 4.19.0 with the new code and on 3.13.0 with the old, with the same three results and the same two stored documents, and then the repository's pytest run.
The reviewer checks that each count_documents keeps the exact filter of the find it replaces, that no call site was left that counts a cursor after sort or limit, which count_documents does not take, and that the mocked mock_mongo fixture in tests/conftest.py is not the only test touching these functions. The description points at that fixture's find.return_value = [] so the reviewer sees why it could not have caught the failure.
What the run does not cover is the running service. app/app.py reads config.cfg and connects to Redis and MongoDB at start-up, and the sandbox starts only the MongoDB it needs for the database layer; the first sign-up on the deployed service after merge is the reviewer's check.
Run it on every error-tracker issue naming a removed PyMongo method
This failure appears at the first request that reaches the changed code, usually long after the install that caused it, so the evidence is an error-tracker issue or a service log. A tracker signs its own webhook deliveries and cannot send the Authorization: Bearer header a Rush webhook trigger needs, so a handler on your server receives the alert, keeps the issues whose title contains object has no attribute 'count' or 'Collection' object is not callable, and forwards the traceback to the trigger's URL with the trigger's secret. Send an Idempotency-Key header to the trigger's URL built from the issue id, so each issue starts one run.
For repositories that have not been deployed since the refresh, search them for .count() on a find( and start one run per repository through the API with the request above, each with a request_id that is a UUID v5 of the repository name, so a repeated sweep returns the runs it already started.
The use case this job follows, with its automation and trigger: Fix a bug from an alert.
Why run it on Rush?
This is a failure that mocks hide. The repository's own tests replace the Mongo client with a MagicMock, so they pass on every PyMongo release, and the laptop that still has PyMongo 3 passes too. A Rush sandbox installs what a fresh deploy would, starts a real MongoDB beside the code, and runs the function that counts, so the error appears where it would in production and the fix is checked against a server rather than a stub, with every command in the record.
The fix is also small enough to get wrong without noticing: count_documents takes a filter and nothing else, so a site that counted after a limit needs a different rewrite, and a review of the diff alone does not show which kind each site is. The pull request carries the before-and-after results of the actual calls, so the reviewer compares outputs rather than reading intent.
Frequently asked questions
Which PyMongo release removed Cursor.count?
PyMongo 4.0, published 2021-11-29. Installed one at a time, 4.0.2 has no count on a cursor, while 3.13.0 still has it and prints a deprecation warning on each call. Collection.count_documents exists in both, so the replacement works before and after the upgrade.
Why did my tests pass before this hit production?
Because they never called PyMongo. In this fixture the mock_mongo fixture replaces the client with a MagicMock whose find returns an empty list, so .count() on it never reaches a real cursor. A mocked database proves the mock, which is why the agent drives the changed functions against a MongoDB it starts in the sandbox.
Is count_documents a drop-in replacement?
For find(filter).count() it is: count_documents(filter) with the same filter. It is a collection method and takes no cursor, so a count after sort, skip or limit has to pass those as keyword arguments instead. The pull request lists each site and which case it was.
What about collection.insert, update and remove?
They are gone in the same major release. On PyMongo 4, calling one of them raises TypeError: 'Collection' object is not callable, with the method named in the message. The agent searches for them alongside the cursor counts; this repository already uses insert_one and find_one everywhere else.
Does the agent need my production database?
No. It starts a MongoDB server inside the sandbox, drops a scratch database, and runs the changed functions against it. Nothing in your deployment is read or changed; the first request after merge is still yours to watch.
More PyMongo jobs
More jobs like this: Fix a bug from an alert
- Fix "d3.scale.linear is not a function" after upgrading D3 past version 3 (D3)
- Fix "database is locked" Errors in a GORM and SQLite App (GORM)
- Fix an ASP.NET Core App Bug From a Datadog Monitor Alert (ASP.NET Core)
- Fix a Django App Bug From a Datadog Monitor Alert (Django)
- Fix an Express API Bug From a Datadog Monitor Alert (Express)