mongodb-conn-cg

1.0.3 • Public • Published

MongoDB Connector for Node.js

This is a simple Node.js library that uses the MongoDB connector to connect to a MongoDB database and to create specific databaseName, collection and You can also insert data

Installation

Install the package using npm:

npm install mongodb-conn-cg

Using this connector you can easily  connect with mongo db

Use the Below code 

// test.js

const { connectToMongoDB } = require('mongodb-conn-cg');

const uri = 'your_mongodb_connection_uri';
const dbName = 'your_mongodb_database_name'; // Replace with your desired database name
const collectionName = 'your_collection_name';

async function main() {
  try {
    const dbConnector = new MongoDBConnector(uri, dbName);
    await dbConnector.connect();

    // Example: Insert a record
    const collection = dbConnector.db.collection(collectionName);
    const recordToInsert = { id: empid, name: 'empName' };
    await collection.insertOne(recordToInsert);
    console.log('Record inserted successfully');

    // Example: Find documents
    const recordsFound = await dbConnector.find(collectionName, {});
    console.log('records found:', recordsFound);

    // Example: Update a record
    const filter = { id: 1 };
    const update = { name: 'updatedName' };
    await dbConnector.update(collectionName, filter, update);
    console.log('Record updated successfully');

    // Example: Delete a record
    const deleteFilter = { id: 10 };
    await dbConnector.delete(collectionName, deleteFilter);
    console.log('Record deleted successfully');

    // Close the connection
    await dbConnector.close();
  } catch (error) {
    console.error('Error:', error);
  }
}

main();

Readme

Keywords

Package Sidebar

Install

npm i mongodb-conn-cg

Weekly Downloads

5

Version

1.0.3

License

ISC

Unpacked Size

5.53 kB

Total Files

4

Last publish

Collaborators

  • anushamoka