mongodb-conn-cg
v1.0.3
Published
connection of mongodb in node js
Downloads
10
Readme
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();