MongoDB NodeJS Cross Instance Cursor
MongoDB Driver Extension allowing to use MongoDB cursors in a micro-service environment by consuming the same cursor accross different NodeJS workers.
Copyright 2023 Crisp IM SAS. See LICENSE for copying information.
Introduction
MongoDB cursors are a powerful way to paginate over millions of results. However, Mongo Drivers are not offering anyway to consume cursors accross different instances. Cursors cannot be resumed after an instance restart or upgrade and this can be problematic in microservice environments
Usually a such tasks are performed using two different solutions:
- Find queries using
limit
and skip
, however, those queries can become unresponsive when paginating over many different results. - Find queries using ranges, however, this can be costly as well when consumming queries accross different indexes.
This project solves all those issues by consuming internal MongoDB APIs that are not exposed by the official MongoDB NodeJS Driver.
This package is using the official MongoDB driver, but relies on internal Mongo Wire commands.
Installation
npm install --save mongodb-cross-cursor
Initiating a cursor
You can use mongodb-cross-cursor
with the regular MongoDB driver (currently only 4.X version is supported).
var MongoCrossCursor = require("mongodb-cross-cursor");
const client = new MongoClient("mongodb://localhost:270017");
const database = client.db("test");
const articles = database.collection("articles");
const instance = await MongoDBCrossCursor.initiate(
articles.find({
published : true
})
);
console.log(instance.sharedCursor)
Resuming a cursor
You can now resume your cursor in a completely different project (for instance a project performing CPU intensive tasks)
var MongoCrossCursor = require("mongodb-cross-cursor");
const client = new MongoClient("mongodb://localhost:270017");
const sharedCursor = {
sessionId : "XXXXXXXX",
cursorId : "YYYYYYYY"
};
const instance = new MongoDBCrossCursor(sharedCursor, client, "test", "articles", 100)
const results = instance.next();
console.log(results)
Iterators
This package also provides iterators
var MongoCrossCursor = require("mongodb-cross-cursor");
const instance = new MongoDBCrossCursor(sharedCursor, client, "test", "articles", 1)
for await (const result of instance.iterate()) {
console.log(result);
}