Skip to content

feat(39): new option batchScanSize - #42

Open
pp0rtal wants to merge 5 commits into
mainfrom
main-39-batch-scan-size
Open

pp0rtal wants to merge 5 commits into
mainfrom
main-39-batch-scan-size

Conversation

@pp0rtal

@pp0rtal pp0rtal commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Issue: #39

Description

On long MDBM migrations, we can encounter socket timeout. This happens when there are no index (linear scan): Mongodb cursor does not filter out any document for minutes, because docs to update are rare or at the end of a collection. We avoid to create indexes for YOLO migrations.
For those long scans, we'd prefer to fetch by small batch (of 1 million for instance)

We need this as a new option batchScanSize:

new MongoBulkDataMigration({
    query: // unchanged (might specify _id here /!\)
    options: {
        batchScanSize: 100_000, // Will scan by 100K docs  
        
        // Specify index in case the one chosen by Mongo can be optimized
        batchScanSize: "company_1__id_1',
    }
})

Note Camille did this on a migration script code directly here I'm reimplementing the logic to make it a native MBDM feature.

Before After
flowchart TD
    A["find(query)<br/>single cursor"] --> B{"next document?"}
    B -- yes --> C["update doc / flush bulk"]
    C --> B
    B -- no --> D("Finalize last bulk")
Loading
  • find() is restricted to a lower/upper bound
  • When the cursor is over, we iterate
flowchart TD
    A["<pre>lower = last upper<br>upper = $gte lower _id + skip(batchScanSize) doc"]
    A --> C["<pre>find(lower ≤ _id < upper)</pre>(cursor)"]
    C --> D{"next document?"}
    D -- yes --> E["update + backup<br/>flush bulk every maxBulkSize"]
    E --> D
    D -- no --> F{"upper reached end?"}:::high_blue
    F -- no --> A
    F -- yes --> H["flush remaining bulk · end"]
    
    classDef high_blue fill:#2567F4,color:#fff,stroke-width:1,stroke:#fff
Loading

Changes

  • Call new method iterateByIdRanges() when options.batchScanSize is specified
    • Support find/aggregate
    • Support queries containing _id already (merge in $and:[...])
  • Add support for options.hint, necessary to specify index now Mongo won't use a perfect matching index

@pp0rtal pp0rtal self-assigned this Oct 3, 2026

@orca-security-eu orca-security-eu Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

@pp0rtal
pp0rtal marked this pull request as ready for review October 5, 2026 07:45

@aurmartin aurmartin left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@pp0rtal Nice! Thanks a lot for this improvement, we will probably use it very soon!

Comment thread src/MongoBulkDataMigration.ts Outdated
} else {
const rangeQuery = _.isEmpty(idRange)
? query
: { ...query, _id: idRange };

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@pp0rtal This will silently override any _id passed in the query. Should we have a warning or even throw an error in that case? Either here or when validation the options (if there is some kind of validation?), the batchScanSize option is not supported if the query is done on _id.

It's properly handled later 👌

@antoine-roth antoine-roth left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@pp0rtal 🚀

@JulienAmoros JulienAmoros left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice!

I spent some time understanding the yield*, but now I get it, very elegant solution 👌


let treatedDocumentsCount = 0;
let document = (await cursor.next()) as WithId<TSchema> | null;
let document = await nextDocument();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[nit] Wouldn't it be better to have a clearer interface for this function?

Suggested change
let document = await nextDocument();
let document = await nextDocument(documents);

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.

4 participants