New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@monti-apm/mongo-sharded-cluster

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@monti-apm/mongo-sharded-cluster

Shard MongoDB in the app layer and build a MongoDB cluster

  • 3.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Mongo Sharded Cluster

A simple way to shard MongoDB on the app layer.

With this approach, we have a set of replica sets. Then we can select a shard for our data based on free disk space on the DB.

For this, you need to run identical clusters (with the same size of disk space and the same storage engine)

Usage

Install it first:

npm i mongo-sharded-cluster

Using it

var MongoCluster = require('mongo-sharded-cluster');
var MongoClient = require('mongodb').MongoClient;
var async = require('async');

var myCluster = new MongoCluster();

var replSetUrls = [
    {name: "one", url: "mongodb://localhost/one"},
    {name: "two", url: "mongodb://localhost/two"},
];
async.each(replSetUrls, function(replSetInfo, done) {
    MongoClient.connect(replSetInfo.url, function(err, db) {
        if(db) {
            myCluster.add(replSetInfo.name, db);
        }
        done(err);
    });
}, function(err) {
    if(err) {
        throw err;
    }

    myCluster.startDbSizeLookup(function() {
        // Pick a shard
        var shardName = myCluster.pickShard(); 
        // Then you can save this shardName in where you need.
        
        // Get the connection for the shard
        var conn = myCluster.getConnection(shardName);
    });
});

Initialize a Cluster With Environment Variables

You can use following helper method to initialize a cluster without hassle.

First make sure you have exports following env variables.

export MONGO_SHARD_URL_one=mongodb://localhost/one
export MONGO_SHARD_URL_two=mongodb://localhost/two

Then you can get initialize a cluster like this:

var MongoCluster = require('mongo-sharded-cluster');
MongoCluster.initFromEnv(function(err, cluster) {
    if(err) {
        throw err;
    }

    // do anything with the cluster
});

Keywords

FAQs

Package last updated on 23 Aug 2022

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