New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

mongoplusplus

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoplusplus

load balancing of read and write operations across multiple MongoDB servers

latest
Source
npmnpm
Version
1.0.9
Version published
Maintainers
1
Created
Source

mongoplusplus

Overview

mongoplusplus is a Node.js package designed to facilitate load balancing of read and write operations across multiple MongoDB databases. It simplifies database connection management, schema definition, model building, and CRUD operations execution.

Installation

Install mongoplusplus via npm:

npm install mongoplusplus

importing

const mongoplusplus = require('mongoplusplus');

##Initialing

const dbname = 'testforUP';

const mongoURI1 = `mongodb+srv://xxxxx:xxxxxx@cluster0.xxxxx.mongodb.net/${dbname}?retryWrites=true&w=majority`;
const mongoURI2 = `readonly:mongodb+srv://xxxxxxx:xxxxxx@cluster0.xxxxxx.mongodb.net/${dbname}?retryWrites=true&w=majority`;
const mongodb = new mongoplusplus([mongoURI1, mongoURI2]);

##connecting database this is the top level thing in your main page (in this test code, under mongodb variable declaration )

(async () => {
  await mongodb.connectToAll();
})();

##Schema defining it is very similar to Mongoose schema definition.but only one mandatory field must be there in the schema which will act as a db identifier for that document

const likeSH = mongodb.Schema({
  user_id: { type: mongoose.Schema.Types.ObjectId, required: true, ref: "users" },
  postId: { type: mongoose.Schema.Types.ObjectId, required: true  },
  dbIndex: { type: Number, required: true },

  like_time: { type: Date, default: Date.now }
}
)

model building

const likes = mongodb.buildModel("likes", likeSH)
Note that dbIndex is used to indicate which MongoDB server it belongs to.
it is a must otherwise it will throw an error.
     throw new Error(`[!]Error :  < dbIndex > must be present in your schema like dbIndex:{
            type: Number,
            required: true
          } `)

usage methods

findInAllDatabase(filter, chain = {})

Usage: Finds documents matching the filter in all MongoDB databases.

const result = await likes.findInAllDatabase({ name: 'John' }, { limit: 10, skip: 0 });
Returns: An array of documents matching the filter from all databases.
writeInAllDatabase(data)

Usage: Writes data to all MongoDB databases.

const result = await likes.writeInAllDatabase({ name: 'John', age: 30 });
Returns: An array of written documents from all databases.
UpdateOneInAllDatabase(filter, update)
Usage: Updates a single document matching the filter in all MongoDB databases.
const updatedDocument = await likes.UpdateOneInAllDatabase({ name: 'John' }, { age: 35 });
Returns: The updated document.
UpdateByIdInAllDatabase(id, update)
Usage: Updates a document by ID in all MongoDB databases.
const updatedDocument = await likes.UpdateByIdInAllDatabase('123456', { age: 35 });
Returns: The updated document.
findByIdInAllDatabaseAndDelete(id)
Usage: Finds a document by ID in all MongoDB databases and deletes it.
const deletedDocument = await likes.findByIdInAllDatabaseAndDelete('123456');
Returns: The deleted document.
findOneInAllDatabaseAndDelete(filter)
Usage: Finds a single document matching the filter in all MongoDB databases and deletes it.
const deletedDocument = await likes.findOneInAllDatabaseAndDelete({ name: 'John' });
Returns: The deleted document.
write(data)
Usage: Writes data to a MongoDB database.
const result = await likes.write({ name: 'John', age: 30 });
Returns: The written document.
bulkWrite(data)
Usage: Bulk writes data to a MongoDB database with transaction inbuilt.

 const testData = Array.from({ length: 10000 }, (_, i) => ({
        name: `User ${i}`,
        email: `user${i}@example.com`,
        age: 20 + (i % 50)
    }));

    // Test 1: Large batch, concurrent
    console.time('Large batch concurrent');
    await UserModel.bulkWrite(testData, {
        batchSize: 5000,
        concurrentBatches: true
    });
findOne(dbIndex, filter, chain = {})
Usage: Finds a single document matching the filter in a specific MongoDB database.
const document = await likes.findOne(0, { name: 'John' }, { limit: 1 });
Returns: The found document.
find(dbIndex, filter, chain = {})
Usage: Finds documents matching the filter in a specific MongoDB database.
const documents = await likes.find(0, { age: { $gt: 18 } }, { limit: 10 });
Returns: An array of found documents.
findById(dbIndex, id, chain = {})
Usage: Finds a document by ID in a specific MongoDB database.
const document = await likes.findById(0, '123456');
Returns: The found document.
findByIdAndUpdate(dbIndex, id, update)
Usage: Finds a document by ID in a specific MongoDB database and updates it.
const updatedDocument = await likes.findByIdAndUpdate(0, '123456', { age: 35 });
Returns: The updated document.
findByIdAndDelete(dbIndex, id)
Usage: Finds a document by ID in a specific MongoDB database and deletes it.
const deletedDocument = await likes.findByIdAndDelete(0, '123456');
Returns: The deleted document.
findOneAndUpdate(dbIndex, filter, update)
Usage: Finds a single document matching the filter in a specific MongoDB database and updates it.

const updatedDocument = await likes.findOneAndUpdate(0, { name: 'John' }, { age: 35 });
Returns: The updated document.
aggregate(dbIndex, filter)
Usage: Aggregates documents in a specific MongoDB database based on the filter.
const aggregationResult = await likes.aggregate(0, [{ $group: { _id: '$name', total: { $sum: '$age' } } }]);
Returns: The aggregation result.
watch(dbIndex)
Usage: Starts watching for changes in a specific MongoDB database.
const watcher = await likes.watch(0);
Returns: A watcher object for the database.

now most of the functionality is as same as mongoose so you can use them directly like indexing a schema

likeSH.index({ user_id: 1, postId: 1 }, { unique: true });

Contributing

there are many things that are not ported. feel free to contribute! Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update.and also

Keywords

MongoDB

FAQs

Package last updated on 05 Jan 2026

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