|
| 1 | +--- |
| 2 | +title: Get started with Azure Cosmos DB MongoDB API and JavaScript |
| 3 | +description: Get started developing a JavaScript application that works with Azure Cosmos DB MongoDB API. This article helps you learn how to set up a project and configure access to an Azure Cosmos DB MongoDB API database. |
| 4 | +author: seesharprun |
| 5 | +ms.author: sidandrews |
| 6 | +ms.service: cosmos-db |
| 7 | +ms.subservice: cosmosdb-mongo |
| 8 | +ms.devlang: javascript |
| 9 | +ms.topic: how-to |
| 10 | +ms.date: 06/23/2022 |
| 11 | +ms.custom: devx-track-js |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +# Get started with Azure Cosmos DB MongoDB API and JavaScript |
| 16 | +[!INCLUDE[appliesto-mongo-api](../includes/appliesto-mongodb-api.md)] |
| 17 | + |
| 18 | +This article shows you how to connect to Azure Cosmos DB MongoDB API using the native MongoDB npm package. Once connected, you can perform operations on databases, collections, and docs. |
| 19 | + |
| 20 | +> [!NOTE] |
| 21 | +> The [example code snippets](https://github.com/Azure-Samples/cosmos-db-mongodb-api-javascript-samples) are available on GitHub as a JavaScript project. |
| 22 | +
|
| 23 | +[MongoDB API reference documentation](https://docs.mongodb.com/drivers/node) | [MongoDB Package (npm)](https://www.npmjs.com/package/mongodb) |
| 24 | + |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | + |
| 28 | +* An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free). |
| 29 | +* [Node.js LTS](https://nodejs.org/en/download/) |
| 30 | +* [Azure Command-Line Interface (CLI)](/cli/azure/) or [Azure PowerShell](/powershell/azure/) |
| 31 | +* [Azure Cosmos DB MongoDB API resource](quickstart-javascript.md#create-an-azure-cosmos-db-account) |
| 32 | + |
| 33 | +## Create a new JavaScript app |
| 34 | + |
| 35 | +1. Create a new JavaScript application in an empty folder using your preferred terminal. Use the [``npm init``](https://docs.npmjs.com/cli/v8/commands/npm-init) command to begin the prompts to create the `package.json` file. Accept the defaults for the prompts. |
| 36 | + |
| 37 | + ```console |
| 38 | + npm init |
| 39 | + ``` |
| 40 | + |
| 41 | +2. Add the [MongoDB](https://www.npmjs.com/package/mongodb) npm package to the JavaScript project. Use the [``npm install package``](https://docs.npmjs.com/cli/v8/commands/npm-install) command specifying the name of the npm package. The `dotenv` package is used to read the environment variables from a `.env` file during local development. |
| 42 | + |
| 43 | + ```console |
| 44 | + npm install mongodb dotenv |
| 45 | + ``` |
| 46 | + |
| 47 | +3. To run the app, use a terminal to navigate to the application directory and run the application. |
| 48 | + |
| 49 | + ```console |
| 50 | + node index.js |
| 51 | + ``` |
| 52 | + |
| 53 | +## Connect with MongoDB native driver to Azure Cosmos DB MongoDB API |
| 54 | + |
| 55 | +To connect with the MongoDB native driver to Azure Cosmos DB, create an instance of the [``MongoClient``](https://mongodb.github.io/node-mongodb-native/4.5/classes/MongoClient.html#connect) class. This class is the starting point to perform all operations against databases. |
| 56 | + |
| 57 | +The most common constructor for **MongoClient** has two parameters: |
| 58 | + |
| 59 | +| Parameter | Example value | Description | |
| 60 | +| --- | --- | --- | |
| 61 | +| ``url`` | ``COSMOS_CONNECTION_STRIN`` environment variable | MongoDB API connection string to use for all requests | |
| 62 | +| ``options`` | `{ssl: true, tls: true, }` | [MongoDB Options](https://mongodb.github.io/node-mongodb-native/4.5/interfaces/MongoClientOptions.html) for the connection. | |
| 63 | + |
| 64 | +Refer to the [Troubleshooting guide](error-codes-solutions.md) for connection issues. |
| 65 | + |
| 66 | +## Get resource name |
| 67 | + |
| 68 | +### [Azure CLI](#tab/azure-cli) |
| 69 | + |
| 70 | +[!INCLUDE [Azure CLI - get resource name](<./includes/azure-cli-get-resource-name.md>)] |
| 71 | + |
| 72 | +### [PowerShell](#tab/azure-powershell) |
| 73 | + |
| 74 | +[!INCLUDE [Powershell - set resource name](<./includes/powershell-set-resource-name.md>)] |
| 75 | + |
| 76 | +### [Portal](#tab/azure-portal) |
| 77 | + |
| 78 | +Skip this step and use the information for the portal in the next step. |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## Retrieve your connection string |
| 83 | + |
| 84 | +### [Azure CLI](#tab/azure-cli) |
| 85 | + |
| 86 | +[!INCLUDE [Azure CLI - get connection string](<./includes/azure-cli-get-connection-string.md>)] |
| 87 | + |
| 88 | +### [PowerShell](#tab/azure-powershell) |
| 89 | + |
| 90 | +[!INCLUDE [Powershell - get connection string](<./includes/powershell-get-connection-string.md>)] |
| 91 | + |
| 92 | +### [Portal](#tab/azure-portal) |
| 93 | + |
| 94 | +> [!TIP] |
| 95 | +> For this guide, we recommend using the resource group name ``msdocs-cosmos``. |
| 96 | + |
| 97 | +[!INCLUDE [Portal - get connection string](<./includes/portal-get-connection-string-from-sign-in.md>)] |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## Configure environment variables |
| 102 | + |
| 103 | +[!INCLUDE [Multitab - store connection string in environment variable](<./includes/environment-variables-connection-string.md>)] |
| 104 | + |
| 105 | +## Create MongoClient with connection string |
| 106 | + |
| 107 | + |
| 108 | +1. Add dependencies to reference the MongoDB and DotEnv npm packages. |
| 109 | + |
| 110 | + :::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/101-client-connection-string/index.js" id="package_dependencies"::: |
| 111 | + |
| 112 | +2. Define a new instance of the ``MongoClient,`` class using the constructor, and [``process.env.``](https://nodejs.org/dist/latest-v8.x/docs/api/process.html#process_process_env) to use the connection string. |
| 113 | + |
| 114 | + :::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/101-client-connection-string/index.js" id="client_credentials"::: |
| 115 | + |
| 116 | +For more information on different ways to create a ``MongoClient`` instance, see [MongoDB NodeJS Driver Quick Start](https://www.npmjs.com/package/mongodb#quick-start). |
| 117 | + |
| 118 | +## Close the MongoClient connection |
| 119 | + |
| 120 | +When your application is finished with the connection remember to close it. That `.close()` call should be after all database calls are made. |
| 121 | + |
| 122 | +```javascript |
| 123 | +client.close() |
| 124 | +``` |
| 125 | + |
| 126 | +## Use MongoDB client classes with Cosmos DB for MongoDB API |
| 127 | + |
| 128 | +[!INCLUDE [Conceptual object model](<./includes/conceptual-object-model.md>)] |
| 129 | + |
| 130 | +Each type of resource is represented by one or more associated JavaScript classes. Here's a list of the most common classes: |
| 131 | + |
| 132 | +| Class | Description | |
| 133 | +|---|---| |
| 134 | +|[``MongoClient``](https://mongodb.github.io/node-mongodb-native/4.5/classes/MongoClient.html)|This class provides a client-side logical representation for the MongoDB API layer on Cosmos DB. The client object is used to configure and execute requests against the service.| |
| 135 | +|[``Db``](https://mongodb.github.io/node-mongodb-native/4.5/classes/Db.html)|This class is a reference to a database that may, or may not, exist in the service yet. The database is validated server-side when you attempt to access it or perform an operation against it.| |
| 136 | +|[``Collection``](https://mongodb.github.io/node-mongodb-native/4.5/classes/Collection.html)|This class is a reference to a collection that also may not exist in the service yet. The collection is validated server-side when you attempt to work with it.| |
| 137 | + |
| 138 | +The following guides show you how to use each of these classes to build your application. |
| 139 | + |
| 140 | +**Guide**: |
| 141 | + |
| 142 | +* [Manage databases](how-to-javascript-manage-databases.md) |
| 143 | +* [Manage collections](how-to-javascript-manage-collections.md) |
| 144 | +* [Manage documents](how-to-javascript-manage-documents.md) |
| 145 | +* [Use queries to find documents](how-to-javascript-manage-queries.md) |
| 146 | + |
| 147 | +## See also |
| 148 | + |
| 149 | +- [Package (NuGet)](https://www.nuget.org/packages/Microsoft.Azure.Cosmos) |
| 150 | +- [API reference](https://docs.mongodb.com/drivers/node) |
| 151 | + |
| 152 | +## Next steps |
| 153 | + |
| 154 | +Now that you've connected to a MongoDB API account, use the next guide to create and manage databases. |
| 155 | + |
| 156 | +> [!div class="nextstepaction"] |
| 157 | +> [Create a database in Azure Cosmos DB MongoDB API using JavaScript](how-to-javascript-manage-databases.md) |
0 commit comments