Skip to content

Commit 8250df9

Browse files
Merge pull request #265414 from kruti-m/main
Pull request to add page of Azure Cosmos -> MongoDB-> VCore-> How To-> Connect using Mongo Shell
2 parents 411460e + 48b66b0 commit 8250df9

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed

articles/cosmos-db/mongodb/vcore/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
items:
5555
- name: Connect using Studio 3T
5656
href: how-to-connect-studio-3t.md
57+
- name: Connect using MongoDB Shell
58+
href: how-to-connect-mongo-shell.md
5759
- name: Migrate data
5860
items:
5961
- name: Premigration assessment
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Use Mongo Shell to connect
3+
titleSuffix: Azure Cosmos DB for MongoDB vCore
4+
description: Connect to an Azure Cosmos DB for MongoDB vCore account using Mongo Shell community tool to query data.
5+
author: kruti-m
6+
ms.author: krmeht
7+
ms.reviewer: yongl
8+
ms.service: cosmos-db
9+
ms.subservice: mongodb-vcore
10+
ms.topic: how-to
11+
ms.date: 02/05/2024
12+
# CustomerIntent: As a database owner, I want to use Mongo Shell to connect and query my database & collections.
13+
---
14+
15+
# Use MongoDB Shell (Mongosh) to connect to Azure Cosmos DB for MongoDB vCore
16+
17+
[!INCLUDE[MongoDB vCore](../../includes/appliesto-mongodb-vcore.md)]
18+
19+
MongoDB Shell (Mongosh), is a JavaScript and Node.js environment for interacting with MongoDB deployments. It's a popular community tool to test queries and interact with the data in your Azure Cosmos DB for MongoDB database.
20+
21+
## Prerequisites
22+
Here are some prerequisites.
23+
- An existing Azure Cosmos DB for MongoDB vCore cluster.
24+
- An installed version of the MongoShell from the community site.
25+
- Ensure you're setting up the necessary environment variables post installation for your operating system
26+
- Ensure the firewall settings to allow the machine to connect. Follow the guidelines for [configuring the firewall for Azure Cosmos DB](../../../cosmos-db/how-to-configure-firewall.md)
27+
- You can choose to allow requests from your current IP, requests from cloud services or requests from virtual machines - specific IP Ranges
28+
29+
![Illustration that shows Firewall Settings update for MongoDB Vcore.](media/how-to-connect-mongo-shell/firewall-settings.gif)
30+
31+
If you accidentally open all the ports, you're warned before saving the changes.
32+
33+
## Connect using Mongo Shell (Mongosh)
34+
35+
To add your Azure Cosmos DB cluster to Mongo shell, perform the following steps:
36+
1. Retrieve the connection information for your Azure Cosmos DB for MongoDB vCore using the instructions [here](quickstart-portal.md#get-cluster-credentials).
37+
38+
![Illustration that shows getting connection string.](./media/how-to-connect-mongo-shell/get-connection-string-portal.gif)
39+
Once you have the connection string, you can either
40+
- Have the shell prompt you to enter the password or
41+
- Provide the password as a part of the connection string
42+
43+
2. Connect using Mongo Shell
44+
45+
**A. By entering the password in the MongoShell Prompt**
46+
47+
48+
Your connection string would look like this:
49+
```
50+
"mongodb+srv://<username>@<servername>.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000"
51+
```
52+
Here's an example of how the command looks like:
53+
```
54+
mongosh "mongodb+srv://[email protected]/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000
55+
```
56+
![Illustration that shows how to connect by entering a password.](./media/how-to-connect-mongo-shell/mongo-shell-connect.gif)
57+
58+
Once you provide the password and are successfully authenticated, you notice some warning. It states *"This server or service appears to be an emulation of MongoDB"*.
59+
The warning can be ignored. It's the shell's way of notifying that you aren't connection to an emulation of MongoDB. When, it's an Azure as a platform as a service offering it's expected.
60+
61+
**B. By providing the password as a part of the connection string**
62+
63+
Alternately you can also use a connection string with the password in which case the format looks something like this.
64+
```
65+
mongosh "mongodb+srv://<SERVERNAME>.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000" --username "<USER>" -password "<PASSWORD>"
66+
```
67+
68+
Here's an example of how the command looks like:
69+
```
70+
mongosh "mongodb+srv://mongodbvcoretesting.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000" --username "testuser" -password "******"
71+
```
72+
![Screenshot that shows a password as a part of a connection string.](./media/how-to-connect-mongo-shell/connection-string-with-password.png)
73+
74+
## Next step
75+
76+
> [!div class="nextstepaction"]
77+
> [Migration options](migration-options.md)
199 KB
Loading
1.22 MB
Loading
380 KB
Loading
178 KB
Loading

0 commit comments

Comments
 (0)