An Azure NoSQL database service for app development.
for Azure Cosmos DB (MongoDB API) you don’t use db.createUser()/mongosh to add Entra-authenticated users—everything is managed at the Azure level. Here’s a quick path you can follow:
Make sure Entra ID authentication is enabled
- In the Azure portal, go to your Cosmos DB for MongoDB vCore cluster → Settings → Authentication
- Under Authentication methods, select both Native DocumentDB and Microsoft Entra ID → Save
- Or use CLI:
az resource patch \
- Under Authentication methods, select both Native DocumentDB and Microsoft Entra ID → Save
--resource-group <rg>
--name <cluster>
--resource-type Microsoft.DocumentDB/mongoClusters
--properties '{"authConfig":{"allowedModes":["NativeAuth","MicrosoftEntraID"]}}'
--latest-include-preview
```
Get the object ID of the Entra principal
- For a user:
```dockerfile
az ad user show --id ******@contoso.com --query objectId -o tsv
```
- For a service principal:
```dockerfile
az ad sp show --id <appId> --query objectId -o tsv
```
- For a managed identity:
```dockerfile
az identity show --resource-group <rg> --name <mi-name> --query principalId -o tsv
```
Register that principal on your cluster with the right roles
- Read-only on database “mydb”:
```json
az resource create \
--resource-group <rg>
--name <cluster>/users/<object-id>
--resource-type Microsoft.DocumentDB/mongoClusters/users
--location <region>
--properties '{
"identityProvider":{"type":"MicrosoftEntraID","properties":{"principalType":"User"}},
"roles":[{"db":"mydb","role":"read"}]
}'
--latest-include-preview
```
- Read-write on “mydb”:
```json
az resource create \
--resource-group <rg>
--name <cluster>/users/<object-id>
--resource-type Microsoft.DocumentDB/mongoClusters/users
--location <region>
--properties '{
"identityProvider":{"type":"MicrosoftEntraID","properties":{"principalType":"User"}},
"roles":[{"db":"mydb","role":"readWrite"}]
}'
--latest-include-preview
```
You can also do this via the portal under Settings → Authentication → + Add Microsoft Entra ID.
Connect using MONGODB-OIDC When you spin up mongosh or MongoDB Compass, use a connection string like:
```yaml
mongodb+srv://<object-id>@<cluster>.mongo.cosmos.azure.com/?tls=true
&authMechanism=MONGODB-OIDC &authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:https://ossrdbms-aad.database.windows.net ```
Replace `<object-id>` with the same principal ID you registered. Cosmos will exchange your Azure AD token for MongoDB access.
I hope this helps. If you have any questions or concerns, please let us know—we're happy to assist further.
Reference docs:
- Configure Microsoft Entra ID authentication: https://dotnet.territoriali.olinfo.it/azure/cosmos-db/mongodb/vcore/how-to-configure-entra-authentication
- Manage Entra ID users on the cluster: https://dotnet.territoriali.olinfo.it/azure/cosmos-db/mongodb/vcore/how-to-configure-entra-authentication#manage-entra-id-users-on-the-cluster
- Connecting with OIDC (Compass/Mongosh): https://dotnet.territoriali.olinfo.it/azure/documentdb/how-to-connect-role-based-access-control#connect-using-microsoft-entra-id-in-mongodb-compass-or-mongodb-shell