Skip to content

Latest commit

 

History

History
155 lines (96 loc) · 7.09 KB

how-to-javascript-get-started.md

File metadata and controls

155 lines (96 loc) · 7.09 KB
title description author ms.author ms.service ms.subservice ms.devlang ms.topic ms.date ms.custom
Get started with Azure Cosmos DB for MongoDB using JavaScript
Get started developing a JavaScript application that works with Azure Cosmos DB for MongoDB. This article helps you learn how to set up a project and configure access to an Azure Cosmos DB for MongoDB database.
seesharprun
sidandrews
cosmos-db
mongodb
javascript
how-to
06/23/2022
devx-track-js, devguide-js, cosmos-db-dev-journey

Get started with Azure Cosmos DB for MongoDB using JavaScript

[!INCLUDEMongoDB]

This article shows you how to connect to Azure Cosmos DB for MongoDB using the native MongoDB npm package. Once connected, you can perform operations on databases, collections, and docs.

Note

The example code snippets are available on GitHub as a JavaScript project.

API for MongoDB reference documentation | MongoDB Package (npm)

Prerequisites

Create a new JavaScript app

  1. Create a new JavaScript application in an empty folder using your preferred terminal. Use the npm init command to begin the prompts to create the package.json file. Accept the defaults for the prompts.

    npm init
  2. Add the MongoDB npm package to the JavaScript project. Use the npm install package 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.

    npm install mongodb dotenv
  3. To run the app, use a terminal to navigate to the application directory and run the application.

    node index.js

Connect with MongoDB native driver to Azure Cosmos DB for MongoDB

To connect with the MongoDB native driver to Azure Cosmos DB, create an instance of the MongoClient class. This class is the starting point to perform all operations against databases.

The most common constructor for MongoClient has two parameters:

Parameter Example value Description
url COSMOS_CONNECTION_STRING environment variable API for MongoDB connection string to use for all requests
options {ssl: true, tls: true, } MongoDB Options for the connection.

Refer to the Troubleshooting guide for connection issues.

Get resource name

[!INCLUDE Azure CLI - get resource name]

[!INCLUDE Powershell - set resource name]

Skip this step and use the information for the portal in the next step.


Retrieve your connection string

[!INCLUDE Azure CLI - get connection string]

[!INCLUDE Powershell - get connection string]

Tip

For this guide, we recommend using the resource group name msdocs-cosmos.

[!INCLUDE Portal - get connection string]


Configure environment variables

[!INCLUDE Multitab - store connection string in environment variable]

Create MongoClient with connection string

  1. Add dependencies to reference the MongoDB and DotEnv npm packages.

    :::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/101-client-connection-string/index.js" id="package_dependencies":::

  2. Define a new instance of the MongoClient class using the constructor, and process.env. to use the connection string.

    :::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/101-client-connection-string/index.js" id="client_credentials":::

For more information on different ways to create a MongoClient instance, see MongoDB NodeJS Driver Quick Start.

Close the MongoClient connection

When your application is finished with the connection, remember to close it. The .close() call should be after all database calls are made.

client.close()

Use MongoDB client classes with Azure Cosmos DB for API for MongoDB

[!INCLUDE Conceptual object model]

Each type of resource is represented by one or more associated JavaScript classes. Here's a list of the most common classes:

Class Description
MongoClient This class provides a client-side logical representation for the API for MongoDB layer on Azure Cosmos DB. The client object is used to configure and execute requests against the service.
Db 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.
Collection 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.

The following guides show you how to use each of these classes to build your application.

Guide:

See also

Next steps

Now that you've connected to an API for MongoDB account, use the next guide to create and manage databases.

[!div class="nextstepaction"] Create a database in Azure Cosmos DB for MongoDB using JavaScript