@simple-node/mongo-connector

1.0.0 • Public • Published

MongoCollection

The MongoCollection class represents a collection in a MongoDB database. It provides methods for performing various operations on the collection such as inserting, finding, updating, and deleting documents.

Constructor

The MongoCollection class constructor takes two parameters: db and collection. It initializes the db and collection properties of the instance.

constructor(db, collection)

Methods

insert(id, data)

Inserts a document into the collection with the specified _id and data.

async insert(id, data)

Example usage:

await dbSessions.insert("session1", { user: "John", lastAccess: new Date() });

find(id, sort = null)

Finds a document in the collection with the specified _id. Optionally, you can provide a sort object to specify the sorting order of the results.

async find(id, sort = null)

Example usage:

const session = await dbSessions.find("session1");

findAll(query, sort = null, startLimit = null, endLimit = null)

Finds all documents in the collection that match the given query. Optionally, you can provide a sort object, start limit, and end limit to control the sorting and pagination of the results.

async findAll(query, sort = null, startLimit = null, endLimit = null)

Example usage:

const sessions = await dbSessions.findAll({ user: "John" }, { lastAccess: -1 }, 0, 10);

delete(id)

Deletes a document from the collection with the specified _id.

async delete(id)

Example usage:

await dbSessions.delete("session1");

deleteAll(query)

Deletes all documents from the collection that match the given query.

async deleteAll(query)

Example usage:

await dbSessions.deleteAll({ user: "John" });

query(query, projection = null, sort = null, startLimit = null, endLimit = null)

Executes a custom query on the collection with the specified query, projection, sort, start limit, and end limit. Returns the result as a cursor.

async query(query, projection = null, sort = null, startLimit = null, endLimit = null)

Example usage:

const cursor = await dbSessions.query({ user: "John" }, { lastAccess: 1 }, { lastAccess: -1 }, 0, 10);

update(query, update)

Updates a document in the collection that matches the given query with the provided update.

async update(query, update)

Example usage:

await dbSessions.update({ _id: "session1" }, { $set: { lastAccess: new Date() } });

updateAll(query, update)

Updates all documents in the collection that match the given query with the provided update.

async updateAll(query, update)

Example usage:

await dbSessions.updateAll({ user: "John" }, { $set: { isActive: false } });

aggregate(query)

Executes an aggregation pipeline on the collection with the specified query. Returns the result as a cursor.

async aggregate(query)

Example usage:

const cursor = await dbSessions.aggregate([
  { $match: { user: "John" } },
  { $group: { _id: "$user", totalSessions: { $sum: 1 } } }
]);

MongoDataBase

The MongoDataBase class represents a MongoDB database. It provides a way to access collections within the database and manages the connection to the MongoDB server.

Constructor

The MongoDataBase class constructor takes two parameters: client and db. It initializes the client and db properties of the instance.

constructor(client, db)

Methods

collection(collection)

Returns a MongoCollection object for the specified collection name. If the collection has already been accessed before, it returns the existing MongoCollection instance.

collection(collection)

Example usage:

const dbSessions = dbUsers.collection('sessions');

Example Usage

Here's an example demonstrating how to use the MongoCollection and MongoDataBase classes:

import { MongoClient } from "mongodb";

// Create a MongoDB client
const dbClient = new MongoClient("mongodb://localhost:27017");

// Create a MongoDB database instance
export const dbUsers = new MongoDataBase(dbClient, 'users');

// Access collections within the database
export const dbSessions = dbUsers.collection('sessions');
export const dbTokens = dbUsers.collection('tokens');
export const dbConfigs = dbUsers.collection('configs');

// Usage example
const sessionData = { user: "John", lastAccess: new Date() };
await dbSessions.insert("session1", sessionData);

const session = await dbSessions.find("session1");
console.log(session);

const sessions = await dbSessions.findAll({ user: "John" }, { lastAccess: -1 }, 0, 10);
console.log(sessions);

await dbSessions.delete("session1");

await dbSessions.update({ _id: "session1" }, { $set: { lastAccess: new Date() } });

This is just a basic example to demonstrate the usage of the classes. You can customize and extend the code according to your specific requirements.

Readme

Keywords

none

Package Sidebar

Install

npm i @simple-node/mongo-connector

Weekly Downloads

2

Version

1.0.0

License

ISC

Unpacked Size

11.1 kB

Total Files

9

Last publish

Collaborators

  • ageofai
  • carnivalofrust
  • darkknight