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

@ozanozkaya/custom-mongodb

v1.0.2

Published

A custom MongoDB module for managing database operations and pagination.

Downloads

80

Readme

@ozanozkaya/custom-mongodb

A custom MongoDB Node.js module designed to simplify database operations such as connecting to MongoDB, data retrieval, updates, deletions, and advanced searches, including fuzzy matching. It supports both single and bulk operations, pagination, and integrated error handling for robust application development.

Features

  • Efficient MongoDB connection management with cached connections.
  • Simple data operations: retrieval, insertion, updating, and deletion.
  • Advanced search: MongoDB Atlas fuzzy text search with customizable options.
  • Pagination support for retrieving data with limit and offset.
  • Bulk operations: Efficiently update or delete multiple documents at once.
  • Integrated error handling for reliable application development.
  • Utility functions: Aggregation, indexing, random distinct document retrieval.

Installation

Install the package using npm:

npm install @ozanozkaya/custom-mongodb

Environment Setup

Ensure your MongoDB credentials and connection string are properly set in your environment variables.

Usage

1. Initialize the MongoDB Manager

To use the package, you first need to initialize it with your MongoDB connection string.

const MongoDBManager = require("@ozanozkaya/custom-mongodb");

// Initialize with your MongoDB connection URL
const manager = new MongoDBManager("yourMongoDBURL");

2. Basic Data Operations

Fetch Data by Query

manager
  .getDataByQuery(
    "dbName",
    "collectionName",
    { yourQuery },
    { projection },
    true
  )
  .then((data) => console.log("Fetched Data:", data))
  .catch((error) => console.error("Error Fetching Data:", error));

Fetch Data with Pagination

manager
  .getDataWithPagination(
    "dbName",
    "collectionName",
    { yourQuery },
    page,
    pageSize
  )
  .then((data) => console.log("Paginated Data:", data))
  .catch((error) => console.error("Error Fetching Data:", error));

Insert Data

manager
  .addData("dbName", "collectionName", { yourData })
  .then((item) => console.log("Data Added:", item))
  .catch((error) => console.error("Error Adding Data:", error));

Update Data by Query

manager
  .findAndUpdateByQuery("dbName", "collectionName", { query }, { dataToUpdate })
  .then((result) => console.log("Data Updated:", result))
  .catch((error) => console.error("Error Updating Data:", error));

Bulk Update Operations

const queryDataPairs = [
  { query: { _id: 1 }, data: { key: "value1" } },
  { query: { _id: 2 }, data: { key: "value2" } },
];
manager
  .bulkFindAndUpdateByQueries("dbName", "collectionName", queryDataPairs)
  .then((result) => console.log("Bulk Update Successful:", result))
  .catch((error) => console.error("Error in Bulk Update:", error));

Delete Data by Query

manager
  .deleteByQuery("dbName", "collectionName", { query })
  .then((result) => console.log("Data Deleted:", result))
  .catch((error) => console.error("Error Deleting Data:", error));

3. Advanced Operations

MongoDB Atlas Fuzzy Search

manager
  .atlasSearch("dbName", "collectionName", "searchTerm", "searchField", true)
  .then((results) => console.log("Search Results:", results))
  .catch((error) => console.error("Error in Search:", error));

Aggregation Pipeline

const pipeline = [{ $match: { status: "active" } }];
manager
  .aggregate("dbName", "collectionName", pipeline)
  .then((results) => console.log("Aggregation Results:", results))
  .catch((error) => console.error("Error Aggregating Data:", error));