npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongodb-cross-cursor

v1.0.8

Published

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

Downloads

5

Readme

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