|
| 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 | + |
| 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 | + |
| 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 | + |
| 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 | + |
| 73 | + |
| 74 | +## Next step |
| 75 | + |
| 76 | +> [!div class="nextstepaction"] |
| 77 | +> [Migration options](migration-options.md) |
0 commit comments