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

mongodb-operators

v1.3.3

Published

A powerful MongoDB manager package that offers advanced CRUD operations, simplified aggregations, and pipeline functionality. It provides utility functions for creating, deleting, and managing MongoDB collections, along with schema definition and database

Downloads

16

Readme

MongoDb Operators

A robust MongoDB manager for Node.js and Next.js, streamlining CRUD, aggregations, and schema management with pipeline support, enhancing MongoDB data handling in applications

Installation

Get started with Installation

  npm install mongodb-operators

Roadmap

  • Establishing and Terminating MongoDB Connections

  • Checking MongoDB Connection Status

  • Creating Collections with Schemas

  • Deleting Collection

  • Creating Document

  • Updating Document/Documents

  • Deleting Document/Documents

  • Find Queries with Limiting and Sorting with timestamp

  • Aggregation Functions/Methods for Single Collections

  • Aggregations Across Collections and Connecting Collections

Environment Variables

Add your mongoDb uri in .env file or hardcode against it

MONGODB_URI

MONGODB_URI format

mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority

Please make sure you add the database_Name in the URI

MongoDB connections: establishing, terminating, and monitoring status.

import { connectToDatabase, disconnectFromDatabase, checkConnectionStatus } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';

const run = async() => {
    try {
        const conn = await connectToDatabase(uri);
        console.log(conn)
        const status = await checkConnectionStatus();
        console.log(status)
    } catch (error) {
        console.error('Error:', error);
    } finally {
        await disconnectFromDatabase();
        const status = await checkConnectionStatus();
        console.log(status)
    }
};

run();

Creating Collections with Schemas

import { createCollection, connectToDatabase } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';

const run = async() => {
    try {
        const data = await connectToDatabase(uri);
        console.log(data)

        const schemaDefinition = {
            message: { type: String, required: true }
        };
        const datas = await createCollection("newCollection", schemaDefinition);
        console.log(datas)

    } catch (error) {
        console.error('Error:', error);
    } finally {

    }
};

run();

Deleting Collections

import { connectToDatabase, deleteCollection } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';

const run = async() => {
    try {
        const data = await connectToDatabase(uri);

        const schemaDefinition = {
            message: { type: String, required: true }
        };
        const deleteResult = await deleteCollection("newCollection");

    } catch (error) {
        console.error('Error:', error);
    } finally {

    }
};

run();

Creating Document

import { connectToDatabase, createDocument } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';

const run = async () => {
    try {
        const db = await connectToDatabase(uri);
        console.log("Connected to database");

        const schemaDefinition = {
            message: { type: String, required: true }
        };

        const collectionName = "users";

        const document = { message: "type your message" };

        
        const result = await createDocument(collectionName, schemaDefinition, document);
        console.log("Document created:", result);

    } catch (error) {
        console.error('Error:', error);
    } finally {
        // Optionally, you can add cleanup code here
    }
};

run();

Updating Document (single document)

import { connectToDatabase, updateDocument } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';

const run = async() => {
    try {

    const data = await connectToDatabase(uri);
    console.log(data);

    const collectionName = "users";

    const schemaDefinition = {
    message: { type: String, required: true }
    };

    const filter = { message: "before text " };
    const update = { message: "updated text" };

    const result = await updateDocument(collectionName, schemaDefinition, filter, update);
    console.log(result);


    } catch (error) {
        console.error('Error:', error);
    } finally {

    }
};

run();

Updating Documents (More than one)

import { connectToDatabase, updateDocuments } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';

const run = async() => {
    try {

    const data = await connectToDatabase(uri);
    console.log(data);

    const collectionName = "users";

    const schemaDefinition = {
    message: { type: String, required: true }
    };

    const filter = { message: "before text " };
    const update = { message: "updated text" };

    const result = await updateDocuments(collectionName, schemaDefinition, filter, update);
    console.log(result);


    } catch (error) {
        console.error('Error:', error);
    } finally {

    }
};

run();

Deleting Document (single document)

import { connectToDatabase, deleteDocument } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';

const run = async() => {
    try {

    const data = await connectToDatabase(uri);
    console.log(data);

    const collectionName = "users";

    const schemaDefinition = {
        message: { type: String, required: true }
    };

    const filter = { message: "type your message" };

    const datas = await deleteDocument(collectionName, schemaDefinition, filter);
    console.log(datas);

    } catch (error) {
        console.error('Error:', error);
    } finally {

    }
};

run();

Deleting Documents (more than one)

import { connectToDatabase, deleteDocuments } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';

const run = async() => {
    try {
        
    const data = await connectToDatabase(uri);
    console.log(data);

    const collectionName = "users";

    const schemaDefinition = {
        message: { type: String, required: true }
    };

    const filter = { message: "type your message" };

    const result = await deleteDocuments(collectionName, schemaDefinition, filter);
    console.log(result);

    } catch (error) {
        console.error('Error:', error);
    } finally {

    }
};

run();

Find Queries with Limiting and Sorting with timestamp

import { findDocument, connectToDatabase } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';

const run = async() => {
    try {
        const data = await connectToDatabase(uri);
        console.log(data)

        const collectionName = "users";

        const schemaDefinition = {
            message: { type: String, required: true }
        };

        const filter = { message: "type your message" };

        // const limit = 0 by default find all docs of specified filter

        const limit = 10 

        const order = 'ascending'

        //sorted based on timestamp
        //passing order is optional

        const result = await findDocument( collectionName, schemaDefinition, filter, limit, order)
        console.log(result)


    } catch (error) {
        console.error('Error:', error);
    } finally {

    }
};

run();

Aggregation Functions/Methods for Single Collections

import { performAggregation, connectToDatabase } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';


const run = async () => {
    try {
        const db = await connectToDatabase(uri);
        console.log("Connected to database:", db);

        const collectionName = 'users';

        const userSchema = {
            name: String,
            age: Number,
            email: { type: String, required: true, unique: true },
        };

        // Aggregation pipeline to calculate average age of users named "john"

        const aggregationPipeline = [
            { $match: { name: "john" } }, // Match documents where name is 'John'
            { $group: { _id: null, averageAge: { $avg: "$age" } } }, // Calculate average age
        ];

        // Perform aggregation using custom function

        const result = await performAggregation(collectionName, userSchema, aggregationPipeline);
        console.log("Average age of people named John:", result);

    } catch (error) {
        console.error("Error:", error);
    } finally {
    }
};

run();

Aggregations Across Collections and Connecting Collections

import { connectToDatabase, aggregateAcrossCollections, disconnectFromDatabase } from "mongodb-operators";

const uri = process.env.MONGODB_URI || 'mongodb+srv://<username>:<password>@<cluster-address>/<database_Name>?retryWrites=true&w=majority';

const run = async () => {
    try {
        const db = await connectToDatabase(uri);
        console.log("Connected to database:", db);

        // Collection names
        // Assuming Both collections have same schema
        const collection1 = 'collection1';
        const collection2 = 'collection2';

        const userSchema = {
            name: String,
            age: Number,
            email: { type: String, required: true, unique: true },
        };

        // Match conditions to filter documents
        const matchConditions = { name: "john", age: 31 };

        // Aggregation pipeline to fetch details from 'collection1' and 'collection2' collections
        const aggregationPipeline = [
            {
                $project: {
                    _id: 1,
                    name: 1,
                    age: 1,
                    email: 1,
                    collection: { $literal: collection1 }, // Adding a field to indicate collection name 'collection1'
                },
            },
            {
                $unionWith: {
                    coll: collection2, // Union with collection 'collection2'
                    pipeline: [
                        {
                            $match: matchConditions,
                        },
                        {
                            $project: {
                                _id: 1,
                                name: 1,
                                age: 1,
                                email: 1,
                                collection: { $literal: collection2 }, // Adding a field to indicate collection name 'collection2'
                            },
                        },
                    ],
                },
            },
        ];

        // Perform aggregation across collections 'collection1' and 'collection2'
        const result = await aggregateAcrossCollections({ [collection1]: userSchema, [collection2]: userSchema }, [collection1, collection2],
            matchConditions,
            aggregationPipeline
        );

        console.log("Aggregation Result:", result);

    } catch (error) {
        console.error("Error:", error);
    } finally {
    }
};

run();