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

mongoplusplus

v1.0.2

Published

load balancing of read and write operations across multiple MongoDB servers

Downloads

16

Readme

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:[email protected]/${dbname}?retryWrites=true&w=majority`;
const mongoURI2 = `readonly:mongodb+srv://xxxxxxx:[email protected]/${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.
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