Tutorial: Use MongoDB with Azure Functions

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

Tutorial: Use MongoDB with Azure Functions

Overview

In this tutorial, you create an HTTP-triggered Azure Function that queries data from MongoDB Atlas by using the MongoDB Node.js driver. You also learn how to manage database connections in a serverless context and deploy your function to the Azure cloud.

Prerequisites

Before you begin, complete the following steps:

Tutorial

This tutorial guides you through the following steps:

  • Create Azure cloud resources.

  • Initialize the local project.

  • Install the MongoDB Node.js driver.

  • Configure the database connection to the sample_mflix database.

  • Write a function to query the sample_mflix.movies collection and retrieve documents representing movies.

  • Test the function locally.

  • Deploy to Azure.

  • Create Azure cloud resources.

Run the following command to create a resource group, replacing <GROUP_NAME> with a name for your group and <AZURE_REGION> with a supported Azure region:

bash az group create \ --name <GROUP_NAME> \ --location <AZURE_REGION>

Optionally, if you are using Azure for the first time, you may need to run the following commands to register the Functions resource provider:

bash az provider register --namespace Microsoft.Storage az provider register --namespace Microsoft.Web

Then, run the following command to create a storage account. Use the same region and group name from the preceding code and provide a unique name for the storage account, replacing the <STORAGE_NAME> placeholder with this unique name:

bash az storage account create \ --name <STORAGE_NAME> \ --location <AZURE_REGION> \ --resource-group <GROUP_NAME> \ --sku Standard_LRS

Finally, run the following command to create the Function app:

bash az functionapp create \ --resource-group <GROUP_NAME> \ --consumption-plan-location <AZURE_REGION> \ --runtime node \ --functions-version 4 \ --name <APP_NAME> \ --storage-account <STORAGE_NAME>

  1. Initialize the local project.

Run the following command to initialize a local Function project:

bash func init MongoExample

When prompted, select node as the worker runtime and javascript as the language.

Run the following commands to navigate into the project directory and create a new HTTP-triggered function:

bash cd MongoExample func new --name GetMovies --template "HTTP trigger"

The command creates the src/functions/GetMovies.js file, which you edit in later steps.

To link your local project to the Azure Function app, run the following command from the project directory:

bash func azure functionapp fetch-app-settings <APP_NAME>

This command downloads all application settings from Azure, including the storage account configuration, and populates your project's local.settings.json file.

  1. Install the MongoDB Node.js Driver.

From the root of your project, run the following command to install the MongoDB Node.js driver:

bash npm install mongodb

This command adds the driver to your project and records it in package.json so that the driver is available when you deploy.

  1. Configure the database connection.

Open src/functions/GetMovies.js and replace its contents with the following code:

```javascript const { app } = require("@azure/functions"); const { MongoClient } = require("mongodb");

const mongoClient = new MongoClient( process.env.MONGODB_ATLAS_URI );

app.http("GetMovies", { methods: ["GET"], authLevel: "anonymous", handler: async (request, context) => { // Query logic added in the next step } });

```

Tip:

Define mongoClient outside the handler function to reuse existing connections across invocations. If you create a new connection inside the handler, each invocation opens a new connection and risks exhausting the maximum number of concurrent connections your database supports. Defining the client at the module level keeps connection counts proportional to traffic.

Depending on your version of Node.js, you may encounter an error related to DNS resolution when you attempt to connect to MongoDB. If this occurs, add the following code to your src/functions/GetMovies.js file before creating the MongoClient instance:

```javascript const { app } = require("@azure/functions"); const { MongoClient } = require("mongodb");

// Forces DNS servers explicitly before connecting to MongoDB require("node:dns/promises").setServers(["1.1.1.1", "8.8.8.8"]);

const mongoClient = new MongoClient( process.env.MONGODB_ATLAS_URI );

app.http("GetMovies", { methods: ["GET"], authLevel: "anonymous", handler: async (request, context) => { // Query logic added in the next step } }); ```

Open local.settings.json and add the following fields to the Values object, replacing each placeholder with your actual values:

json { "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "<storage-connection-string>", "FUNCTIONS_WORKER_RUNTIME": "node", "MONGODB_ATLAS_URI": "<atlas-connection-string>", "MONGODB_ATLAS_DATABASE": "sample_mflix", "MONGODB_ATLAS_COLLECTION": "movies" }, "ConnectionStrings": {} }

To retrieve your MongoDB Atlas connection string, navigate to the Atlas dashboard, select your cluster, and click Connect. To learn how to retrieve your Azure storage connection string, see Configure a connection string in the Azure documentation.

  1. Write a function to query MongoDB.

In src/functions/GetMovies.js, replace the // Query logic added in the next step comment inside the handler with the following code:

javascript try { const database = mongoClient.db( process.env.MONGODB_ATLAS_DATABASE ); const collection = database.collection( process.env.MONGODB_ATLAS_COLLECTION ); const results = await collection .find({}) .limit(10) .toArray(); return { jsonBody: results }; } catch (error) { return { status: 500, jsonBody: { message: "Internal server error." } }; }

When the function receives a request, it connects to the database and collection specified by your environment variables and returns up to 10 documents. If an error occurs, the function returns a 500 response that contains the error message.

  1. Test the function locally.

From the root of your project, run the following command to start your function:

bash func start

When the server starts, the CLI displays the local URL for your function. Navigate to http://localhost:7071/api/GetMovies in your browser or send a request by using a tool such as cURL or Postman.

Note:

If the server starts but you cannot retrieve data, verify that your local IP address is included in the Atlas network access list. The Azure data center IP rules you configured for production do not apply when testing locally. For more information, see [Configure IP Access List Entries.](https://www.mongodb.com/docs/atlas/security/ip-access-list.md#std-label-add-to-access-list

  1. Deploy to Azure.

Set your environment variables in Azure by running the following commands, replacing each placeholder with your actual values:

```bash az functionapp config appsettings set \ --name \ --resource-group \ --settings MONGODB_ATLAS_URI=

az functionapp config appsettings set \ --name \ --resource-group \ --settings MONGODB_ATLAS_DATABASE=sample_mflix

az functionapp config appsettings set \ --name \ --resource-group \ --settings MONGODB_ATLAS_COLLECTION=movies ```

Deploy your function by running the following command:

bash func azure functionapp publish <APP_NAME>

Output:

bash Deployment successful. Remote build succeeded! Syncing triggers... Functions in <APP_NAME>: GetMovies - [httpTrigger] Invoke url: https://<APP_NAME>.azurewebsites.net/api/getmovies

When the deployment completes, the CLI displays a public URL for your function. Because the function uses authLevel: “anonymous”, it is accessible without a key.

Note:

For production deployments, consider changing authLevel to “function” or “admin” and distributing the appropriate key to authorized clients. To learn more, see the Work with access keys in Azure Functions documentation.

Learn More

To learn more about the concepts covered in this tutorial, see the following resources:

  • See [Manage Connections with Azure Functions](https://www.mongodb.com/docs/atlas/manage-connections-azure-functions.md#std-label-atlas-azure-connections in the Atlas documentation to learn about connection best practices

  • See the Azure Functions documentation to learn more about Functions