This module is a simple wrapper around the MongoDB Node.js driver, designed specifically for use with AWS Lambda. It provides easy-to-use functions to manage connections to a MongoDB Atlas cluster from within a serverless environment.
This wrapper simplifies the process of connecting your AWS Lambda functions to a MongoDB Atlas database. It manages the database connection, making it easy to perform CRUD operations without worrying about connection pooling or handling connection timeouts.
Description: Establishes a connection to the MongoDB Atlas cluster.
await mongoDBWrapper.connect();
Description: Closes the connection to the MongoDB Atlas cluster.
await mongoDBWrapper.disconnect();
Description: Executes a provided function within a MongoDB transaction.
await mongoDBWrapper.executeInTransaction(async (session) => {
// Your transactional code here
});
- Parameters: func - A function to be executed within the transaction.
Description: Inserts a single document into a specified collection.
await mongoDBWrapper.insertOne('collectionName', document);
- Parameters: collectionName - The name of the collection where the document will be inserted. doc - The document to be inserted.
Description: Inserts multiple documents into a specified collection.
await mongoDBWrapper.insertMany('collectionName', documents);
- Parameters: collectionName - The name of the collection where the documents will be inserted. docs - An array of documents to be inserted.
Description: Finds documents in a specified collection based on a query filter.
const documents = await mongoDBWrapper.find('collectionName', queryOptions);
- Parameters: collectionName - The name of the collection to query. queryOptions - An object containing query options such as filter, skip, limit, sort, and projection.
Description: Finds a single document in a specified collection based on a query filter.
const document = await mongoDBWrapper.findOne('collectionName', queryOptions);
- Parameters: collectionName - The name of the collection to query. queryOptions - An object containing query options such as filter and projection.
Description: Updates a single document in a specified collection based on a query filter.
const result = await mongoDBWrapper.updateOne('collectionName', filter, update);
- Parameters: collectionName - The name of the collection to update. filter - The filter criteria to identify the document to update. update - The update operations to apply to the document.
Description: Deletes a single document from a specified collection based on a query filter.
const result = await mongoDBWrapper.deleteOne('collectionName', filter);
- Parameters: collectionName - The name of the collection to delete from. filter - The filter criteria to identify the document to delete.