# mongo-iterator

> A small utility for efficiently iterating through MongoDb collections

Latest version **0.1.6** (published 2020-08-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongo-iterator
pnpm add mongo-iterator
yarn add mongo-iterator
bun add mongo-iterator
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.6 |
| Published | 2020-08-25 |
| First published | 2020-04-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 8.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Stevan Dedovic |
| Maintainers | sdedovic |
| Keywords | mongodb, cursor, database, data, db, migration, migrate, foreach, tool, utility |

## Links

- npm: https://www.npmjs.com/package/mongo-iterator
- Repository: https://github.com/sdedovic/mongo-iterator-ts
- Homepage: https://github.com/sdedovic/mongo-iterator-ts#readme
- Issues: https://github.com/sdedovic/mongo-iterator-ts/issues
- npm.io page: https://npm.io/package/mongo-iterator

## Dependencies (2)

- [verror](https://npm.io/package/verror.md) ^1.10.0
- [mongodb](https://npm.io/package/mongodb.md) ^3.5.5

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.1.6 (latest) — 2020-08-25
- 0.1.5 — 2020-07-20
- 0.1.4 — 2020-05-26
- 0.1.3 — 2020-05-22
- 0.1.2 — 2020-05-08
- 0.1.1 — 2020-05-08
- 0.1.0 — 2020-05-08
- 0.0.7 — 2020-05-08
- 0.0.6 — 2020-05-08
- 0.0.5 — 2020-04-21
- 0.0.4 — 2020-04-21
- 0.0.3 — 2020-04-21
- 0.0.2 — 2020-04-21
- 0.0.1 — 2020-04-21

## README

# Mongo Iterator
A small utility for efficiently iterating through MongoDb collections.

![](https://github.com/sdedovic/mongo-iterator-ts/workflows/Node.js%20Package/badge.svg)
![npm (tag)](https://img.shields.io/npm/v/mongo-iterator/latest)

```bash
npm i --save mongo-iterator
```

This library exposes a single class `MongoIterator`. The constructor takes a Mongo cursor and callback. The callback is executed with a batch of documents from the cursor. Conceptually it functions as a `forEach` loop.

Under the hood a `MongoIterator` object gathers a single bach from Mongo before invoking the callback. This means it avoids unecessary network requests. It is useful for iterating over large collections for ETL jobs or for updating documents en masse.

**This library is in a work in progress. Breaking changes are inbound. YMMV.**

## Dependencies
**Tested with:**
- Mongo 3.4.6
- NodeJs 12.x

## Usage
### Basic Example

```typescript
import { MongoClient } from "mongodb";
import { MongoIterator } from "./src";

async function main(): Promise<void> {
    const client = new MongoClient(...);
    await client.connect();
    const cursor = client
        .db('')
        .collection('')
        .find({})
        .sort({_id: 1})
        .batchSize(2000);

    const iterator = new MongoIterator(cursor, async batch => {
        // do something with the batch
    });

    await iterator.run();

    // log/inspect iterator.getMetrics();
}
```

## Documentation
Just read the code. It's short.

### TODO
- Publish to NPM with type defs
- Add unit tests to repo after I fix them
- Test/un-deprecate the `pause`/`resume` methods.

---
_Source: https://npm.io/package/mongo-iterator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
