Skip to content

Commit 247efa6

Browse files
authored
Merge pull request #299669 from qianwens/main
[serviceconnector]add tutorial for mongodb atlas
2 parents 7bc52a9 + 3a03cf9 commit 247efa6

File tree

2 files changed

+242
-0
lines changed

2 files changed

+242
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
title: Integrate MongoDB Atlas cluster with Service Connector
3+
description: Integrate MongoDB Atlas cluster into your application with Service Connector.
4+
author: qianwens
5+
ms.author: qianwens
6+
ms.service: service-connector
7+
ms.topic: how-to
8+
ms.custom: engagement-fy23
9+
ms.date: 05/08/2025
10+
---
11+
12+
# Integrate MongoDB Atlas cluster with Service Connector
13+
14+
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.
15+
16+
## Supported compute services
17+
18+
Service Connector can be used to connect the following compute services to MongoDB Atlas cluster:
19+
20+
- Azure App Service
21+
- Azure Container Apps
22+
- Azure Functions
23+
- Azure Kubernetes Service (AKS)
24+
25+
## Supported authentication types and client types
26+
27+
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.
28+
29+
| Client type | System-assigned managed identity | User-assigned managed identity | Secret/connection string | Service principal |
30+
|---------------------------|:--------------------------------:|:------------------------------:|:------------------------:|:-----------------:|
31+
| .NET | No | No | Yes | No |
32+
| Go (pg) | No | No | Yes | No |
33+
| Java (JDBC) | No | No | Yes | No |
34+
| Java - Spring Boot (JDBC) | No | No | Yes | No |
35+
| Node.js (pg) | No | No | Yes | No |
36+
| PHP (native) | No | No | Yes | No |
37+
| Python (psycopg2) | No | No | Yes | No |
38+
| Python-Django | No | No | Yes | No |
39+
| Ruby (ruby-pg) | No | No | Yes | No |
40+
| None | No | No | Yes | No |
41+
42+
43+
## Default environment variable names or application properties and sample code
44+
45+
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](concept-service-connector-internals.md#configuration-naming-convention) article.
46+
47+
### Connection String
48+
49+
> [!WARNING]
50+
> 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.
51+
52+
#### [.NET](#tab/dotnet)
53+
54+
| Default environment variable name | Description | Example value |
55+
| ------------------------------------- | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
56+
| `MONGODBATLAS_CLUSTER_CONNECTIONSTRING` | .NET MongoDB Atlas connection string | `mongodb+srv://myUser:[email protected]/?retryWrites=true&w=majority&appName=Cluster0` |
57+
58+
#### [Java](#tab/java)
59+
60+
| Default environment variable name | Description | Example value |
61+
| ------------------------------------- | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
62+
| `MONGODBATLAS_CLUSTER_CONNECTIONSTRING` | JDBC MongoDB Atlas connection string | `jdbc:mongodb+srv://myUser:[email protected]/?retryWrites=true&w=majority&appName=Cluster0` |
63+
64+
#### [Python](#tab/python)
65+
66+
| Default environment variable name | Description | Example value |
67+
| ------------------------------------- | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
68+
| `MONGODBATLAS_CLUSTER_CONNECTIONSTRING` | Python MongoDB Atlas connection string | `mongodb+srv://myUser:[email protected]/?retryWrites=true&w=majority&appName=Cluster0` |
69+
70+
#### [Django](#tab/django)
71+
72+
| Default environment variable name | Description | Example value |
73+
| ------------------------------------- | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
74+
| `MONGODBATLAS_CLUSTER_CONNECTIONSTRING` | Django MongoDB Atlas connection string | `mongodb+srv://myUser:[email protected]/?retryWrites=true&w=majority&appName=Cluster0` |
75+
76+
#### [Go](#tab/go)
77+
78+
| Default environment variable name | Description | Example value |
79+
|-------------------------------------|---------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
80+
| `MONGODBATLAS_CLUSTER_CONNECTIONSTRING` | Go MongoDB Atlas connection string | `mongodb+srv://myUser:[email protected]/?retryWrites=true&w=majority&appName=Cluster0` |
81+
82+
#### [NodeJS](#tab/nodejs)
83+
84+
| Default environment variable name | Description | Example value |
85+
|-------------------------------------|---------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
86+
| `MONGODBATLAS_CLUSTER_CONNECTIONSTRING` | NodeJS MongoDB Atlas connection string | `mongodb+srv://myUser:[email protected]/?retryWrites=true&w=majority&appName=Cluster0` |
87+
88+
#### [PHP](#tab/php)
89+
90+
| Default environment variable name | Description | Example value |
91+
|-----------------------------------|--------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
92+
| `MONGODBATLAS_CLUSTER_CONNECTIONSTRING` | PHP native MongoDB Atlas connection string | `mongodb+srv://myUser:[email protected]/?retryWrites=true&w=majority&appName=Cluster0` |
93+
94+
#### [Ruby](#tab/ruby)
95+
96+
| Default environment variable name | Description | Example value |
97+
|-----------------------------------|---------------------------------|----------------------------------------------------------------------------------|
98+
| `MONGODBATLAS_CLUSTER_CONNECTIONSTRING` | Ruby MongoDB Atlas connection string | `mongodb+srv://myUser:[email protected]/?retryWrites=true&w=majority&appName=Cluster0` |
99+
100+
#### [Other](#tab/none)
101+
102+
| Default environment variable name | Description | Example value |
103+
|-----------------------------------|---------------------------------|----------------------------------------------------------------------------------|
104+
| `MONGODBATLAS_CLUSTER_CONNECTIONSTRING` | MongoDB Atlas connection string | `mongodb+srv://myUser:[email protected]/?retryWrites=true&w=majority&appName=Cluster0` |
105+
106+
---
107+
108+
#### Sample code
109+
110+
Refer to the steps and code below to connect to MongoDB Atlas cluster using a connection string.
111+
[!INCLUDE [code sample for mongodb secrets](./includes/code-mongodb-atlas-secret.md)]
112+
113+
114+
## Next steps
115+
116+
Follow the tutorials listed below to learn more about Service Connector.
117+
118+
> [!div class="nextstepaction"]
119+
> [Learn about Service Connector concepts](./concept-service-connector-internals.md)
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
author: wchigit
3+
ms.service: service-connector
4+
ms.topic: include
5+
ms.date: 12/04/2023
6+
ms.author: wchi
7+
---
8+
9+
### [.NET](#tab/dotnet)
10+
11+
1. Install dependency.
12+
```bash
13+
dotnet add package MongoDb.Driver
14+
```
15+
16+
2. Get the connection string from the environment variable added by Service Connector and connect to MongoDB Atlas.
17+
```csharp
18+
using MongoDB.Driver;
19+
20+
var connectionString = Environment.GetEnvironmentVariable("MONGODBATLAS_CLUSTER_CONNECTIONSTRING");
21+
var client = new MongoClient(connectionString);
22+
```
23+
24+
25+
### [Java](#tab/java)
26+
27+
1. Add the following dependency in your *pom.xml* file:
28+
```xml
29+
<dependency>
30+
<groupId>org.mongodb</groupId>
31+
<artifactId>mongo-java-driver</artifactId>
32+
<version>3.4.2</version>
33+
</dependency>
34+
```
35+
36+
1. Get the connection string from the environment variable added by Service Connector and connect to MongoDB Atlas.
37+
38+
```java
39+
import com.mongodb.MongoClient;
40+
import com.mongodb.MongoClientURI;
41+
import com.mongodb.client.MongoCollection;
42+
import com.mongodb.client.MongoDatabase;
43+
import com.mongodb.client.model.Filters;
44+
45+
String connectionString = System.getenv("MONGODBATLAS_CLUSTER_CONNECTIONSTRING");
46+
MongoClientURI uri = new MongoClientURI(connectionString);
47+
48+
MongoClient mongoClient = null;
49+
try {
50+
mongoClient = new MongoClient(uri);
51+
} finally {
52+
if (mongoClient != null) {
53+
mongoClient.close();
54+
}
55+
}
56+
```
57+
58+
### [Python](#tab/python)
59+
1. Install dependency.
60+
```bash
61+
pip install pymongo
62+
```
63+
64+
2. Get the connection string from the environment variable added by Service Connector and connect to MongoDB Atlas.
65+
```python
66+
import os
67+
import pymongo
68+
69+
conn_str = os.environ.get("MONGODBATLAS_CLUSTER_CONNECTIONSTRING")
70+
client = pymongo.MongoClient(conn_str)
71+
```
72+
73+
### [Go](#tab/go)
74+
1. Install dependency.
75+
```bash
76+
go get go.mongodb.org/mongo-driver/mongo
77+
```
78+
2. Get the connection string from the environment variable added by Service Connector and connect to MongoDB Atlas.
79+
```go
80+
import (
81+
"context"
82+
"fmt"
83+
"log"
84+
"os"
85+
86+
"go.mongodb.org/mongo-driver/bson"
87+
"go.mongodb.org/mongo-driver/mongo"
88+
"go.mongodb.org/mongo-driver/mongo/options"
89+
)
90+
91+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
92+
defer cancel()
93+
94+
mongoDBConnectionString = os.Getenv("MONGODBATLAS_CLUSTER_CONNECTIONSTRING")
95+
clientOptions := options.Client().ApplyURI(mongoDBConnectionString).SetDirect(true)
96+
97+
c, err := mongo.Connect(ctx, clientOptions)
98+
if err != nil {
99+
log.Fatalf("unable to initialize connection %v", err)
100+
}
101+
102+
err = c.Ping(ctx, nil)
103+
if err != nil {
104+
log.Fatalf("unable to connect %v", err)
105+
}
106+
```
107+
108+
### [NodeJS](#tab/nodejs)
109+
1. Install dependency.
110+
```bash
111+
npm install mongodb
112+
```
113+
2. Get the connection string from the environment variable added by Service Connector and connect to MongoDB Atlas.
114+
```javascript
115+
const { MongoClient, ObjectId } = require('mongodb');
116+
117+
const url = process.env.MONGODBATLAS_CLUSTER_CONNECTIONSTRING;
118+
const client = new MongoClient(url);
119+
```
120+
121+
122+
### [Other](#tab/none)
123+
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](../how-to-integrate-mongodb-atlas.md).

0 commit comments

Comments
 (0)