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

@simple-node/mongo-connector

v1.0.0

Published

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.

Downloads

2

Readme

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.