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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongodb-ops

v0.11.1

Published

Read and write ops for MongoDB

Readme

mongodb-ops

Lightweight read/write helpers for MongoDB with a shared, self-managing client. The MongoDB client is cached at the class (process) level per connection string and reused across calls — you connect once and let the driver handle pooling, topology monitoring, and failover recovery.

writeConcern is not configurable through this library.

Install

npm install mongodb-ops

Requires Node.js 16+ and bundles the official mongodb driver (v6).

Exports

const { MongoDBOps, MongoDBToolSet } = require('mongodb-ops');
  • MongoDBOps — low-level static/instance operations (getData, search, writeData, writeBulkData, getCollectionCount, getDbClient, closeDBConn, getObjectId).
  • MongoDBToolSet — a higher-level, collection-scoped convenience class that extends MongoDBOps (get / insert / update / replace / delete + bulk variants).

Quick start

Collection-scoped (MongoDBToolSet)

const { MongoDBToolSet } = require('mongodb-ops');

const connString = "mongodb+srv://USER:[email protected]/your-db?retryWrites=true&w=majority";

// Instance style — bind a collection + connection once
const orders = new MongoDBToolSet('orders', connString);

await orders.insertOne({ number: 'PO-1001', status: 'open' });
const open = await orders.getDataByFilter({ status: 'open' }, { number: 1 }, { number: -1 });
await orders.updateOne({ $set: { status: 'closed' } }, { number: 'PO-1001' });

Every method also has a static form that takes the collection name and connection string explicitly — handy when wrapping it in your own model class:

class Order extends MongoDBToolSet {
  static collectionName = 'orders';
  static connString = connString;

  static getById(id) {
    return MongoDBToolSet.getDataByID(this.collectionName, id, undefined, this.connString);
  }
}

Low-level (MongoDBOps)

const { MongoDBOps } = require('mongodb-ops');

const rows = await MongoDBOps.getData(
  'orders',
  { status: 'open' },              // query
  false,                           // isAggregate
  { number: 1 },                   // projection
  { number: -1 },                  // sort
  { startIndex: 1, endIndex: 20 }, // pagination (1-based, inclusive)
  false,                           // isGetCount
  connString
);

Connection management

getDbClient(connString) maintains one cached MongoClient per connection string for the life of the process (kept in an internal Map; concurrent first-connects are de-duplicated). All operations reuse it, so there is no per-call connect overhead — and the driver's topology monitoring transparently rediscovers a new primary after a replica-set failover.

Clients are created with safe, failover-friendly defaults:

{ serverSelectionTimeoutMS: 8000, retryWrites: true, retryReads: true }

Override or extend the driver options process-wide, before the first connection is made — your values are merged over the defaults:

const { MongoDBOps } = require('mongodb-ops');
MongoDBOps.clientOptions = { maxPoolSize: 20 };

Close every cached connection (e.g. in a CLI or test teardown; long-running servers/Lambdas normally leave them open for reuse):

await MongoDBOps.closeDBConn();

API

MongoDBToolSet

Constructor: new MongoDBToolSet(collectionName, connString). Instance methods use the bound collection/connection; each has a matching static method that takes them as arguments.

| Read | Write | Bulk | |---|---|---| | getDataByID | insertOne | insertBulkOrdered / insertBulkUnOrdered | | getDataByFilter | replaceOne | replaceBulkOrdered / replaceBulkUnOrdered | | getDataByAggregate | updateOne / updateMany | updateBulkOrdered / updateBulkUnOrdered | | getDataCount | deleteOne / deleteMany | deleteBulkOrdered / deleteBulkUnOrdered | | list (rows + optional total) | | allBulkOrdered / allBulkUnOrdered | | getAllData | | |

MongoDBOps

getData, search, writeData(type, …), writeBulkData(type, …, ordered), getCollectionCount, getObjectId, getDbClient, closeDBConn.

Changelog

0.11.1

  • Hardened connection caching. The client cache is now keyed by connection string in a Map (previously matched against MongoDB driver internals), making client reuse reliable across driver versions.
  • De-duplicated concurrent cold-start connects by caching the connect promise; a failed connect is evicted so the next call retries instead of caching a rejected promise.
  • Safer driver defaults: serverSelectionTimeoutMS: 8000, retryWrites: true, retryReads: true — reads now recover automatically across a replica-set failover/election.
  • New MongoDBOps.clientOptions hook to override/extend the driver options process-wide.
  • closeDBConn() now drains the client Map (and any legacy cache).

0.11.0

  • Added estimated collection count (getCollectionCount).

Earlier

  • search, collation, and aggregate options. See the git history for details.

License

ISC