{"id":15645873,"url":"https://github.com/alexellis/mongodb-function","last_synced_at":"2025-06-15T23:03:44.635Z","repository":{"id":87754014,"uuid":"126954666","full_name":"alexellis/mongodb-function","owner":"alexellis","description":"OpenFaaS Function that makes use of a connection pool to access MongoDB","archived":false,"fork":false,"pushed_at":"2018-03-27T15:44:22.000Z","size":56,"stargazers_count":53,"open_issues_count":3,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-30T10:50:00.041Z","etag":null,"topics":["databases","faas","functions","openfaas","serverless"],"latest_commit_sha":null,"homepage":"https://www.openfaas.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexellis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-27T08:30:32.000Z","updated_at":"2025-01-07T05:24:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"41d97293-d768-49d3-9a22-fbf6fa226ced","html_url":"https://github.com/alexellis/mongodb-function","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexellis%2Fmongodb-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexellis%2Fmongodb-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexellis%2Fmongodb-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexellis%2Fmongodb-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexellis","download_url":"https://codeload.github.com/alexellis/mongodb-function/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251685289,"owners_count":21627269,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["databases","faas","functions","openfaas","serverless"],"created_at":"2024-10-03T12:10:25.762Z","updated_at":"2025-04-30T10:50:02.374Z","avatar_url":"https://github.com/alexellis.png","language":"JavaScript","readme":"# OpenFaaS with MongoDB\n\nThis is a simple example of how to use connection pooling in MongoDB with OpenFaaS on Kubernetes.\n\n![](./diagram/conceptual-mongo.png)\n\n1. In the first sequence we've had no calls made to the function, so the connection pool is not yet initialized. `prepareDB()` was never called.\n\n2. In the second sequence `prepareDB()` has been called and since there was no instance of a connection in memory, we create one and you see the dotted line shows the connection being established. This will then open a connection to MongoDB in the network.\n\n3. In the third sequence we see that subsequent calls detect a connection exists and go straight to the connection pool. We have one active connection and two shown with dotted lines which are about to be closed due to inactivity.\n\n## Pre-requisites\n\nBefore we can build and deploy the example we'll set up OpenFaaS on Kubernetes or Swarm followed by MongoDB. This configuration is suitable for development and testing.\n\n1. Start by cloning the Github repository:\n\n```\n$ git clone https://github.com/alexellis/mongodb-function\n```\n\n### Kubernetes\n\n1. Install OpenFaaS with `helm`\n\nhttps://docs.openfaas.com/deployment/kubernetes/\n\n2. Install the OpenFaaS CLI\n\n```bash\ncurl -sL https://cli.openfaas.com | sudo sh\n```\n\n3. Set your `OPENFAAS_URL` variable\n\n```\n$ export OPENFAAS_URL=127.0.0.1:31112\n```\n\nIf you're using minikube or a remote machine then replace 127.0.0.1 with that IP address.\n\n4. Install mongodb via `helm`\n\n```shell\n$ helm install stable/mongodb --name openfaas-db \\\n  --namespace openfaas-fn \\\n  --set persistence.enabled=false\n```\n\nNote down the name of the MongoDB instance i.e. `openfaas-db-mongodb`\n\n\u003e If you want to use the fully-qualified DNS name that would be: `openfaas-db-mongodb.openfaas-fn.svc.cluster.local.`\n\nNow skip ahead to \"Build and test\"\n\n### Docker Swarm\n\n1. Install OpenFaaS with Docker\n\nhttps://docs.openfaas.com/deployment/docker-swarm/\n\n2. Install the OpenFaaS CLI\n\n```bash\ncurl -sL https://cli.openfaas.com | sudo sh\n```\n\n3. Set your `OPENFAAS_URL` variable\n\n```\n$ export OPENFAAS_URL=127.0.0.1:8080\n```\n\n4. Create a mongodb Docker Service\n\n```\n$ docker service create --network=func_functions --name openfaas-db-mongodb --publish 27017:27017 mongo mongod\n```\n\nThe entry for the stack.yml file will be the IP of your Docker Swarm manager.\n\n### Build and test\n\n1. Update your stack.yml's mongo field with the MongoDB DNS entry/IP from prior steps\n\n2. Replace \"alexellis/\" prefix from Docker Hub in stack.yml with your own account\n\n3. Build/push/deploy\n\nPull in the *node8-express* template:\n\n```\n$ faas template pull https://github.com/openfaas-incubator/node8-express-template\n```\n\nNow build and push / deploy\n\n```\n$ faas build \u0026\u0026 faas push \u0026\u0026 faas deploy\n```\n\n4. Get a load-testing tool\n\nThis requires a [local installation of Go](https://golang.org/dl/).\n\n```\n$ go get -u github.com/rakyll/hey\n```\n\nAn alternative tool would be Apache-Bench which is available for most Linux distributions via a package manager.\n\n5. Run a test\n\nLet's start by running a single request with `curl`:\n\n```\n$ curl http://$OPENFAAS_URL/function/insert-user \\\n  --data-binary '{\"first\":\"Alex\", \"last\": \"Ellis\"}' \\\n  -H \"Content-Type: application/json\"\n```\n\nNow run a load-test with `hey`:\n\n```\n$ ~/go/bin/hey -m POST -d '{\"first\":\"Alex\", \"last\": \"Ellis\"}' \\\n  -H \"Content-Type: application/json\" \\\n  -n 1000 -c 10 http://$OPENFAAS_URL/function/insert-user\n```\n\nThis test posts in a JSON body with 1000 requests with 10 of those being concurrent.\n\nHere's an abridged output from `hey` with the function running on a remote server with the test being run from my laptop:\n\n```\nSummary:\n  Requests/sec: 1393.2083\n...\nStatus code distribution:\n  [200] 10000 responses\n```\n\nIf you look at the logs of the Mongo deployment or service you will see the connection count is between 1-10 connections only during the load-test. This shows that the connection-pool is being used by our function.\n\nOn Kubernetes:\n\n```\n$ kubectl logs deploy/openfaas-db-mongodb -f -n openfaas-fn\n```\n\nOn Swarm:\n\n```\n$ docker service logs openfaas-db-mongodb -f\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexellis%2Fmongodb-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexellis%2Fmongodb-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexellis%2Fmongodb-function/lists"}