add limit support to mongodb driver#1480
Merged
Merged
Conversation
Contributor
Reviewer's GuideAdds optional limit support to the MongoDB driver query and fetch methods and extends the release workflow to build and publish macOS wheels, along with a patch version bump. Sequence diagram for MongoDriver.query with optional limitsequenceDiagram
participant Client
participant MongoDriver
participant Database
participant Collection
participant Cursor
Client->>MongoDriver: query(collection_name, query, args, limit, kwargs)
MongoDriver->>MongoDriver: _test_connection()
MongoDriver->>MongoDriver: _select_database()
MongoDriver->>Database: get database
Database-->>MongoDriver: db
MongoDriver->>Database: db[collection_name]
Database-->>MongoDriver: collection
MongoDriver->>Collection: find(query or {}, args, kwargs)
Collection-->>MongoDriver: cursor
alt limit is provided
MongoDriver->>Cursor: limit(limit)
Cursor-->>MongoDriver: cursor
end
MongoDriver->>Cursor: to_list(length=None)
Cursor-->>MongoDriver: documents
MongoDriver->>MongoDriver: _serializer(documents, None)
MongoDriver-->>Client: serialized_result
Sequence diagram for MongoDriver.fetch with optional limitsequenceDiagram
participant Client
participant MongoDriver
participant Database
participant Collection
participant Cursor
Client->>MongoDriver: fetch(collection_name, query, args, limit, kwargs)
MongoDriver->>MongoDriver: _test_connection()
MongoDriver->>MongoDriver: _select_database()
MongoDriver->>Database: get database
Database-->>MongoDriver: db
MongoDriver->>Database: db[collection_name]
Database-->>MongoDriver: collection
MongoDriver->>Collection: find(query or {}, args, kwargs)
Collection-->>MongoDriver: cursor
alt limit is provided
MongoDriver->>Cursor: limit(limit)
Cursor-->>MongoDriver: cursor
end
loop iterate cursor
MongoDriver->>Cursor: get next document
Cursor-->>MongoDriver: document
MongoDriver->>MongoDriver: append to result list
end
MongoDriver-->>Client: (result, None)
Class diagram for MongoDB driver limit supportclassDiagram
class MongoDriver {
+query(collection_name str, query dict, *args, limit int, **kwargs) Iterable
+fetch(collection_name str, query dict, *args, limit int, **kwargs) Iterable
_select_database() Database
_serializer(data Any, schema Any) Any
}
class Database {
+__getitem__(collection_name str) Collection
}
class Collection {
+find(query dict, *args, **kwargs) Cursor
}
class Cursor {
+limit(count int) Cursor
+to_list(length int) list
+__aiter__() Cursor
}
MongoDriver --> Database : uses
Database --> Collection : returns
Collection --> Cursor : returns
MongoDriver --> Cursor : iterates
note for MongoDriver "query and fetch now accept optional limit to constrain MongoDB cursor"
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey there - I've reviewed your changes - here's some feedback:
- In both
queryandfetch, theif limit:check will skip a validlimit=0value; consider usingif limit is not None:to distinguish betweenNoneand falsy numeric limits. - The
queryandfetchmethods now duplicate thecollection.find(...); cursor = cursor.limit(limit)logic; consider extracting this into a small helper to keep the behavior consistent and reduce repetition.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In both `query` and `fetch`, the `if limit:` check will skip a valid `limit=0` value; consider using `if limit is not None:` to distinguish between `None` and falsy numeric limits.
- The `query` and `fetch` methods now duplicate the `collection.find(...); cursor = cursor.limit(limit)` logic; consider extracting this into a small helper to keep the behavior consistent and reduce repetition.
## Individual Comments
### Comment 1
<location> `asyncdb/drivers/mongo.py:516-519` </location>
<code_context>
collection_name: str,
query: Optional[dict] = None,
*args,
+ limit: Optional[int] = None,
**kwargs
) -> Iterable[Any]:
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Use an explicit `is not None` check for `limit` to avoid surprising behavior with `0`.
Using `if limit:` will also skip a computed `limit=0` or other falsy values. An explicit `if limit is not None:` makes the behavior unambiguous and prevents silent misconfiguration when `limit` is derived rather than hard-coded.
```suggestion
cursor = collection.find(query or {}, *args, **kwargs)
if limit is not None:
cursor = cursor.limit(limit)
result = await cursor.to_list(length=None)
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
phenobarbital
added a commit
that referenced
this pull request
Mar 20, 2026
add limit support to mongodb driver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by Sourcery
Add support for limiting result sets in the MongoDB driver and extend release workflows to build and publish macOS wheels.
New Features:
Enhancements:
Build: