Skip to content

add limit support to mongodb driver#1480

Merged
phenobarbital merged 2 commits into
masterfrom
or_conditions
Dec 10, 2025
Merged

add limit support to mongodb driver#1480
phenobarbital merged 2 commits into
masterfrom
or_conditions

Conversation

@phenobarbital
Copy link
Copy Markdown
Owner

@phenobarbital phenobarbital commented Dec 10, 2025

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:

  • Allow specifying a limit on documents returned by the MongoDB driver's query method.
  • Allow specifying a limit on documents returned by the MongoDB driver's fetch method.

Enhancements:

  • Bump library version from 2.13.0 to 2.13.1 to reflect the new MongoDB driver capabilities and packaging updates.

Build:

  • Update release workflow to build wheels for macOS on both x86_64 and arm64 architectures and publish them to PyPI.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Dec 10, 2025

Reviewer's Guide

Adds 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 limit

sequenceDiagram
    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
Loading

Sequence diagram for MongoDriver.fetch with optional limit

sequenceDiagram
    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)
Loading

Class diagram for MongoDB driver limit support

classDiagram
    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"
Loading

File-Level Changes

Change Details Files
Add optional limit parameter to MongoDB driver's query and fetch methods to constrain result set size.
  • Extend query method signature to accept an optional integer limit parameter
  • Apply limit to the MongoDB cursor when a limit value is provided before materializing results
  • Extend fetch method signature with optional limit and apply it to the cursor iteration path
asyncdb/drivers/mongo.py
Extend CI release workflow to build, detect, and publish macOS wheels alongside existing Linux and Windows artifacts.
  • Add macOS-latest to the GitHub Actions build matrix
  • Configure cibuildwheel to build macOS wheels for x86_64 and arm64 architectures
  • Detect macOS wheels in the dist directory and set an environment flag
  • Upload macOS wheels to PyPI when present, mirroring existing Linux and Windows upload steps
.github/workflows/release.yml
Bump library patch version to reflect new behavior and packaging support.
  • Increment asyncdb version from 2.13.0 to 2.13.1
asyncdb/version.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • 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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread asyncdb/drivers/mongo.py
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@phenobarbital phenobarbital merged commit 5c8a702 into master Dec 10, 2025
3 checks passed
@phenobarbital phenobarbital deleted the or_conditions branch December 10, 2025 23:49
phenobarbital added a commit that referenced this pull request Mar 20, 2026
add limit support to mongodb driver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant