Edit

Share via


Integrate MongoDB Atlas cluster with Service Connector

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

Sample code

Refer to the steps and code below to connect to MongoDB Atlas cluster using a connection string.

  1. Install dependency.

    dotnet add package MongoDb.Driver
    
  2. 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);
    

Next steps

Follow the tutorials listed below to learn more about Service Connector.