Skip to content

Add BatchSize parameter to MongoDB source to prevent cursor timeouts on large collections#234

Open
Copilot wants to merge 4 commits intomainfrom
copilot/fix-mongo-cursor-error
Open

Add BatchSize parameter to MongoDB source to prevent cursor timeouts on large collections#234
Copilot wants to merge 4 commits intomainfrom
copilot/fix-mongo-cursor-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 8, 2026

Migrating MongoDB collections with 250k+ documents fails with MongoCursorNotFoundException because cursors timeout before all documents are fetched.

Changes

  • MongoSourceSettings: Added optional BatchSize property to control documents per batch
  • MongoRepository: Updated FindAsync to apply BatchSize via FindOptions when specified
  • MongoDataSourceExtension: Modified both filtered and unfiltered collection reads to use FindAsync with batch size support (previously unfiltered reads used AsQueryable() which doesn't support batching)
  • Documentation: Added BatchSize parameter details and examples to MongoDB extension README

Usage

{
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "mydb",
    "Collection": "large_collection",
    "BatchSize": 1000
}

If not specified, MongoDB's default batch size applies. Negative or zero values are silently ignored to prevent migration failures.

Original prompt

This section details on the original issue you should resolve

<issue_title>MongoCursorNotFoundException: Cursor not found on server using connection.</issue_title>
<issue_description>Hello!

I am performing a data migration from an un-sharded Azure Cosmos MongoDB (source) account to a sharded Azure Cosmos MongoDB account (sink). One of my collections in the source has ~250k documents. When running the dmt tool, I am running into a cursor not found exception when migrating this particular collection:

Data transfer failed
MongoDB.Driver.MongoCursorNotFoundException: Cursor <id> not found on server <fqdn:10255> using connection <connection_id>

The community forum has a few recommendation as mentioned here. I have tried to reduce BatchSize but in vain. I would like to have the ability to disable cursor timeouts. Is this possible to implement with the current implementation of the C# driver?

In addition, I've always noticed that this exception gets thrown at the 18000th record exact (I have reproduced this 3 times). That being said, I am not sure if it's really due to 10 minute cursor timeout...

Please advise. Your support is appreciated :)</issue_description>

<agent_instructions>I started working on implementing what I believe is the fix of adding BatchSize to the Source but I never finished. Take a look at this branch: https://github.com/philnach/data-migration-desktop-tool/tree/MongoDBBatchSize for the work that I started. Please add BatchSize to the MongoDB source and make sure documentation and tests are updated accordingly. Also review your code for accuracy.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@philnach I believe the issue is that the sink doesn't consider the ```BatchSize``` only the source. I'm working on plumbing ```BatchSize``` in (https://github.com/philnach/data-migration-desktop-tool/tree/MongoDBBatchSize).

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits January 8, 2026 20:13
…rrors

Co-authored-by: philnach <19275540+philnach@users.noreply.github.com>
Co-authored-by: philnach <19275540+philnach@users.noreply.github.com>
Co-authored-by: philnach <19275540+philnach@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix MongoCursorNotFoundException during data migration Add BatchSize parameter to MongoDB source to prevent cursor timeouts on large collections Jan 8, 2026
Copilot AI requested a review from philnach January 8, 2026 20:21
@philnach
Copy link
Copy Markdown
Collaborator

philnach commented Jan 8, 2026

@copilot Is this a good solution for largo MongoDB collecitons? Does the MongoDB drive/sdk support this scenario already?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 8, 2026

@copilot Is this a good solution for largo MongoDB collecitons? Does the MongoDB drive/sdk support this scenario already?

Yes, this is a good solution for large MongoDB collections. The MongoDB C# driver (v2.30.0) natively supports the BatchSize parameter through FindOptions, which is exactly what we're using in the implementation.

How it works:

  • The FindOptions.BatchSize property controls how many documents the server returns in each batch when iterating through a cursor
  • This is a standard MongoDB feature that prevents cursor timeouts on large result sets
  • The driver automatically manages cursor lifecycle and fetches new batches as needed

Why this solves the problem:

  • Without BatchSize, cursors can timeout (default: 10 minutes) if the client doesn't fetch documents quickly enough
  • For 250k+ document collections, setting BatchSize: 1000 ensures documents are fetched in manageable chunks, keeping the cursor active
  • This is the recommended MongoDB approach for handling large datasets

Alternative approaches considered:

  1. Using AsQueryable() - doesn't support batch size control, can timeout
  2. Increasing cursor timeout - not configurable from client side in Azure Cosmos DB
  3. Pagination with skip/limit - inefficient for large offsets

The implementation follows MongoDB best practices and leverages native driver functionality.

@philnach philnach marked this pull request as ready for review January 10, 2026 19:47
@philnach
Copy link
Copy Markdown
Collaborator

@markjbrown / @bowencode , this is an older issue, but it enables a scenario where there's large data coming from MongoDB. It allows the user to specify the BatchSize against the source. dmt already supported against the sink.

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.

MongoCursorNotFoundException: Cursor not found on server using connection.

2 participants