Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongodb-cross-cursor

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongodb-cross-cursor

A mongodb driver extension allowing to consume MongoDB cursors accross multiple instances

  • 1.0.7
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

MongoDB NodeJS Cross Instance Cursor

Test and Build Build and Release Version Downloads

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");

// Use the regular Mongo client
const client    = new MongoClient("mongodb://localhost:270017");
const database  =  client.db("test");
const articles  = database.collection("articles");

const instance = await MongoDBCrossCursor.initiate(
  // Regular MongoDB find command
  articles.find({
    published : true
  })
);

// A shared cursor is now available. You can re-use this cursor in a different worker
// {
//    sessionId : "XXXXXXXX",
//    cursorId  : "YYYYYYYY"
// }
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");

// Use the regular Mongo client
const client    = new MongoClient("mongodb://localhost:270017");

// The cursor we just created above
const sharedCursor = {
  sessionId : "XXXXXXXX",
  cursorId  : "YYYYYYYY"
};

// Resume a cross cursor instance using the sharedCursor object and a 100 batch Size
const instance = new MongoDBCrossCursor(sharedCursor, client, "test", "articles", 100)

// We can now fetch results, by iterating the cursor.
const results = instance.next();

// It should return 100 results.
console.log(results)

Iterators

This package also provides iterators

var MongoCrossCursor = require("mongodb-cross-cursor");

// Resume a cross cursor instance with a 1 batch size
const instance = new MongoDBCrossCursor(sharedCursor, client, "test", "articles", 1)

// We can now fetch results, by iterating the cursor.
for await (const result of instance.iterate()) {
  console.log(result);
}

FAQs

Package last updated on 03 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc