This page shows supported authentication methods and clients, and shows sample code you can use to connect MongoDB Atlas cluster from Azure compute services using Service Connector. You might still be able to connect to MongoDB Atlas cluster in other programming languages without using Service Connector. This page also shows default environment variable names and values you get when you create the service connection.
Supported compute services
Service Connector can be used to connect the following compute services to MongoDB Atlas cluster:
- Azure App Service
- Azure Container Apps
- Azure Functions
- Azure Kubernetes Service (AKS)
Supported authentication types and client types
The table below shows which combinations of authentication methods and clients are supported for connecting your compute service to MongoDB Atlas cluster using Service Connector. A “Yes” indicates that the combination is supported, while a “No” indicates that it is not supported.
Client type |
System-assigned managed identity |
User-assigned managed identity |
Secret/connection string |
Service principal |
.NET |
No |
No |
Yes |
No |
Go (pg) |
No |
No |
Yes |
No |
Java (JDBC) |
No |
No |
Yes |
No |
Java - Spring Boot (JDBC) |
No |
No |
Yes |
No |
Node.js (pg) |
No |
No |
Yes |
No |
PHP (native) |
No |
No |
Yes |
No |
Python (psycopg2) |
No |
No |
Yes |
No |
Python-Django |
No |
No |
Yes |
No |
Ruby (ruby-pg) |
No |
No |
Yes |
No |
None |
No |
No |
Yes |
No |
Default environment variable names or application properties and sample code
Reference the connection details and sample code in the following tables, according to your connection's authentication type and client type, to connect compute services to MongoDB Atlas cluster. For more information about naming conventions, check the Service Connector internals article.
Connection String
Warning
Microsoft recommends that you use the most secure authentication flow available. The authentication flow described in this procedure requires a very high degree of trust in the application, and carries risks that are not present in other flows. You should only use this flow when other more secure flows, such as managed identities, aren't viable.
Default environment variable name |
Description |
Example value |
MONGODBATLAS_CLUSTER_CONNECTIONSTRING |
.NET MongoDB Atlas connection string |
mongodb+srv://<database-username>:<database-password>@<cluster-URL>/?retryWrites=true&w=majority&appName=Cluster0 |
Default environment variable name |
Description |
Example value |
MONGODBATLAS_CLUSTER_CONNECTIONSTRING |
JDBC MongoDB Atlas connection string |
jdbc:mongodb+srv://<database-username>:<database-password>@<cluster-URL>/?retryWrites=true&w=majority&appName=Cluster0 |
Default environment variable name |
Description |
Example value |
MONGODBATLAS_CLUSTER_CONNECTIONSTRING |
Python MongoDB Atlas connection string |
mongodb+srv://<database-username>:<database-password>@<cluster-URL>/?retryWrites=true&w=majority&appName=Cluster0 |
Default environment variable name |
Description |
Example value |
MONGODBATLAS_CLUSTER_CONNECTIONSTRING |
Django MongoDB Atlas connection string |
mongodb+srv://<database-username>:<database-password>@<cluster-URL>/?retryWrites=true&w=majority&appName=Cluster0 |
Default environment variable name |
Description |
Example value |
MONGODBATLAS_CLUSTER_CONNECTIONSTRING |
Go MongoDB Atlas connection string |
mongodb+srv://<database-username>:<database-password>@<cluster-URL>/?retryWrites=true&w=majority&appName=Cluster0 |
Default environment variable name |
Description |
Example value |
MONGODBATLAS_CLUSTER_CONNECTIONSTRING |
NodeJS MongoDB Atlas connection string |
mongodb+srv://<database-username>:<database-password>@<cluster-URL>/?retryWrites=true&w=majority&appName=Cluster0 |
Default environment variable name |
Description |
Example value |
MONGODBATLAS_CLUSTER_CONNECTIONSTRING |
PHP native MongoDB Atlas connection string |
mongodb+srv://<database-username>:<database-password>@<cluster-URL>/?retryWrites=true&w=majority&appName=Cluster0 |
Default environment variable name |
Description |
Example value |
MONGODBATLAS_CLUSTER_CONNECTIONSTRING |
Ruby MongoDB Atlas connection string |
mongodb+srv:/<database-username>:<database-password>@<cluster-URL>/?retryWrites=true&w=majority&appName=Cluster0 |
Default environment variable name |
Description |
Example value |
MONGODBATLAS_CLUSTER_CONNECTIONSTRING |
MongoDB Atlas connection string |
mongodb+srv://<database-username>:<database-password>@<cluster-URL>/?retryWrites=true&w=majority&appName=Cluster0 |
Sample code
Refer to the steps and code below to connect to MongoDB Atlas cluster using a connection string.
Install dependency.
dotnet add package MongoDb.Driver
Get the connection string from the environment variable added by Service Connector and connect to MongoDB Atlas.
using MongoDB.Driver;
var connectionString = Environment.GetEnvironmentVariable("MONGODBATLAS_CLUSTER_CONNECTIONSTRING");
var client = new MongoClient(connectionString);
Add the following dependency in your pom.xml file:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
Get the connection string from the environment variable added by Service Connector and connect to MongoDB Atlas.
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
String connectionString = System.getenv("MONGODBATLAS_CLUSTER_CONNECTIONSTRING");
MongoClientURI uri = new MongoClientURI(connectionString);
MongoClient mongoClient = null;
try {
mongoClient = new MongoClient(uri);
} finally {
if (mongoClient != null) {
mongoClient.close();
}
}
Install dependency.
pip install pymongo
Get the connection string from the environment variable added by Service Connector and connect to MongoDB Atlas.
import os
import pymongo
conn_str = os.environ.get("MONGODBATLAS_CLUSTER_CONNECTIONSTRING")
client = pymongo.MongoClient(conn_str)
- Install dependency.
go get go.mongodb.org/mongo-driver/mongo
- Get the connection string from the environment variable added by Service Connector and connect to MongoDB Atlas.
import (
"context"
"fmt"
"log"
"os"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
mongoDBConnectionString = os.Getenv("MONGODBATLAS_CLUSTER_CONNECTIONSTRING")
clientOptions := options.Client().ApplyURI(mongoDBConnectionString).SetDirect(true)
c, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatalf("unable to initialize connection %v", err)
}
err = c.Ping(ctx, nil)
if err != nil {
log.Fatalf("unable to connect %v", err)
}
- Install dependency.
npm install mongodb
- Get the connection string from the environment variable added by Service Connector and connect to MongoDB Atlas.
const { MongoClient, ObjectId } = require('mongodb');
const url = process.env.MONGODBATLAS_CLUSTER_CONNECTIONSTRING;
const client = new MongoClient(url);
For other languages, you can use the MongoDB resource endpoint and other properties that Service Connector sets to the environment variables to connect to MongoDB Atlas. For environment variable details, see Integrate MongoDB with Service Connector.
Next steps
Follow the tutorials listed below to learn more about Service Connector.