# Lambda , node.js and mongoDB

**URL:** https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007
**Category:** Serverless Framework
**Created:** [December 25, 2016, 8:58pm UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007 "2016-12-25T20:58:04Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![abhijitkilledar](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@abhijitkilledar](https://forum.serverless.com/u/abhijitkilledar)
#### Post date: [December 25, 2016, 8:58pm UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/1 "2016-12-25T20:58:04Z")

</div>

Does anyone have sample code for node.js, mongoDB using mongoose? I need to do CRUD on mongo collection and I would like to use lambda and serverless.

---

<div class="post-metadata">

### Author: ![DavidWells](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/davidwells/32/201_2.png) [@DavidWells](https://forum.serverless.com/u/DavidWells)
#### Post date: [December 26, 2016, 9:55pm UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/2 "2016-12-26T21:55:32Z")

</div>

Checkout the [https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb](https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb) example.

It’s using dynamoDB but you could replace the get/post/delete functions with your calls to your remote MongoDB table.

You will need a hosted mongoDB solution because the lambda function node environment only lives for 5minutes max.

---

<div class="post-metadata">

### Author: ![DavidWells](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/davidwells/32/201_2.png) [@DavidWells](https://forum.serverless.com/u/DavidWells)
#### Post date: [December 26, 2016, 9:57pm UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/3 "2016-12-26T21:57:57Z")

</div>

You will need to connect to your remote MongoDB. Checkout [http://mongoosejs.com/docs/connections.html](http://mongoosejs.com/docs/connections.html)

---

<div class="post-metadata">

### Author: ![abhijitkilledar](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@abhijitkilledar](https://forum.serverless.com/u/abhijitkilledar)
#### Post date: [December 28, 2016, 1:28am UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/4 "2016-12-28T01:28:01Z")

</div>

Hello David,

Appreciate your help on above answer. dynamaDB example is very helpful.

I am trying to see if code for connecting to DB I can put somewhere in initialize code of lambda. I know all the threads says that we should not rely that container will be reused but just trying to see if I get lot of concurrent requests lambda container might get reused. So instead of creating mongo connection for every request only during container initialization I can get connection.  
Is there a way in lambda that some code block will get executed only during lambda container initialization ?

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [December 28, 2016, 2:36am UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/5 "2016-12-28T02:36:38Z")

</div>

As I understand it, the code outside of the handler function is executed once. You could connect to the database there but with container reuse there’s no guarantee the connection would still be around when the handler is executed. If you have a way to test the connection is still active prior to using it then that might be viable solution.

---

<div class="post-metadata">

### Author: ![lucianopf](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/lucianopf/32/220_2.png) [@lucianopf](https://forum.serverless.com/u/lucianopf)
#### Post date: [December 28, 2016, 5:46pm UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/6 "2016-12-28T17:46:02Z")

</div>

Hey mate, I understand you, and I’ve been where u’re at. Heheheh

I lost a lot of time trying to implement it and I finally found a useful way of doing it, it’s not the optimal way of doing it but it works pretty well…

Since I’m not using Serverless as the framework, I’m using ClaudiaJS deploying a full ExpressJS application. For every request recieved I do connect to the database and disconnect from the database. (using middleware)

The problem that I was facing with mongoose was that I was unable to keep a connection, I’ve tried several things and none but this worked.

(I’ve done it before with serverless also but that was before the new version of it and it’s not useful anymore)

Github project(under development) link: [https://github.com/TCEES-NTI/SAPO](https://github.com/TCEES-NTI/SAPO)

---

<div class="post-metadata">

### Author: ![abhijitkilledar](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@abhijitkilledar](https://forum.serverless.com/u/abhijitkilledar)
#### Post date: [January 1, 2017, 8:33pm UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/7 "2017-01-01T20:33:41Z")

</div>

> [@lucianopf](#):
>
> ClaudiaJS

thanks lucianopf for sharing your github repo.  
how was your experience with ClaudiaJS as compare to serverless framework? why did you move ClaudiaJS? I am new to serverless and still trying to find out whats best option for development for lambda and AWS api gateway.

---

<div class="post-metadata">

### Author: ![rowanu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rowanu/32/75_2.png) [@rowanu](https://forum.serverless.com/u/rowanu)
#### Post date: [January 2, 2017, 3:57am UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/8 "2017-01-02T03:57:16Z")

</div>

> [@abhijitkilledar](#):
>
> Is there a way in lambda that some code block will get executed only during lambda container initialization ?

I wrote about that [in this blog post](http://blog.rowanudell.com/database-connections-in-lambda/), hope it helps.

---

<div class="post-metadata">

### Author: ![abhijitkilledar](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@abhijitkilledar](https://forum.serverless.com/u/abhijitkilledar)
#### Post date: [January 2, 2017, 4:30am UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/9 "2017-01-02T04:30:07Z")

</div>

rowanu, thanks for sharing very nicely written article.

---

<div class="post-metadata">

### Author: ![lucianopf](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/lucianopf/32/220_2.png) [@lucianopf](https://forum.serverless.com/u/lucianopf)
#### Post date: [April 19, 2017, 11:29am UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/10 "2017-04-19T11:29:50Z")

</div>

That is an awesome point, and I’ve been deeply trying to do this, since this is a thing to take care since the release of the lambda functions.

The problem is that mongo won’t let us have multiple connections, so you can’t predict in which context that you’ll be and inevitactbly you’ll invoke a new context that will try to make a new connection with the mongo.

Unfortunetly the only solution that I could think of is to release the connection after every request =(

If someone know’s a way to open multiple connections on a lambda function I’ll be really happy to hear!

---

<div class="post-metadata">

### Author: ![kimo](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kimo](https://forum.serverless.com/u/kimo)
#### Post date: [June 5, 2017, 6:45am UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/11 "2017-06-05T06:45:24Z")

</div>

In case someone finds this post here goes some useful sample code  
[https://github.com/serverless/examples/tree/master/aws-node-rest-api-mongodb](https://github.com/serverless/examples/tree/master/aws-node-rest-api-mongodb)

---

<div class="post-metadata">

### Author: ![rlyle](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@rlyle](https://forum.serverless.com/u/rlyle)
#### Post date: [October 8, 2018, 9:00pm UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/12 "2018-10-08T21:00:15Z")

</div>

@DavidWells I hate people who answer questions with a different solution, it’s NOT helpful!

dynamodb is nothing better than a redis key/value store, no query support, no aggregation, don’t bother answer freaking questions if you don’t know the damn answer.

---

<div class="post-metadata">

### Author: ![DavidWells](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/davidwells/32/201_2.png) [@DavidWells](https://forum.serverless.com/u/DavidWells)
#### Post date: [October 9, 2018, 1:03am UTC](https://forum.serverless.com/t/lambda-node-js-and-mongodb/1007/13 "2018-10-09T01:03:04Z")

</div>

Here is a mongo example for ya [https://github.com/maciejmatu/smoothielicious/blob/b7a5a76eb29af076ed228c9b5d190fb56d0df1d2/src/lambda/add-page-visit.js](https://github.com/maciejmatu/smoothielicious/blob/b7a5a76eb29af076ed228c9b5d190fb56d0df1d2/src/lambda/add-page-visit.js)

Have a wonderful day
