AWS Lambda is a popular service for running arbitrary functions without managing individual servers. Using Mongoose in your AWS Lambda functions is easy. Here's a sample function that connects to a MongoDB instance and finds a single document:
constmongoose=require('mongoose');letconn=null;consturi='YOUR CONNECTION STRING HERE';exports.handler=asyncfunction(event,context){// Make sure to add this so you can re-use `conn` between function calls.// See https://www.mongodb.com/blog/post/serverless-development-with-nodejs-aws-lambda-mongodb-atlascontext.callbackWaitsForEmptyEventLoop=false;// Because `conn` is in the global scope, Lambda may retain it between// function calls thanks to `callbackWaitsForEmptyEventLoop`.// This means your Lambda function doesn't have to go through the// potentially expensive process of connecting to MongoDB every time.if(conn==null){conn=mongoose.createConnection(uri,{// and tell the MongoDB driver to not wait more than 5 seconds// before erroring out if it isn't connectedserverSelectionTimeoutMS: 5000});// `await`ing connection after assigning to the `conn` variable// to avoid multiple function calls creating new connectionsawaitconn.asPromise();conn.model('Test',newmongoose.Schema({name: String}));}constM=conn.model('Test');constdoc=awaitM.findOne();console.log(doc);returndoc;};
The above code works fine for a single Lambda function, but what if you want to reuse the same connection logic in multiple Lambda functions? You can export the below function.
constmongoose=require('mongoose');letconn=null;consturi='YOUR CONNECTION STRING HERE';exports.connect=asyncfunction(){if(conn==null){conn=mongoose.createConnection(uri,{serverSelectionTimeoutMS: 5000});// `await`ing connection after assigning to the `conn` variable// to avoid multiple function calls creating new connectionsawaitconn.asPromise();}returnconn;};
You can also use mongoose.connect()
, so you can use mongoose.model()
to create models.
constmongoose=require('mongoose');letconn=null;consturi='YOUR CONNECTION STRING HERE';exports.connect=asyncfunction(){if(conn==null){conn=mongoose.connect(uri,{serverSelectionTimeoutMS: 5000}).then(()=>mongoose);// `await`ing connection after assigning to the `conn` variable// to avoid multiple function calls creating new connectionsawaitconn;}returnconn;};