Quickstart: Use Azure Cosmos DB for MongoDB (RU) with Node.js
Article
Applies to:
✅ MongoDB (RU)
In this quickstart, you deploy a basic Azure Cosmos DB for MongoDB application using Python. Azure Cosmos DB for MongoDB is a schemaless data store allowing applications to store unstructured documents in the cloud with MongoDB libraries. You learn how to create documents and perform basic tasks within your Azure Cosmos DB resource using Python.
If you don't have an Azure account, create a free account before you begin.
Initialize the project
Use the Azure Developer CLI (azd) to create an Azure Cosmos DB for Table account and deploy a containerized sample application. The sample application uses the client library to manage, create, read, and query sample data.
Open a terminal in an empty directory.
If you're not already authenticated, authenticate to the Azure Developer CLI using azd auth login. Follow the steps specified by the tool to authenticate to the CLI using your preferred Azure credentials.
During initialization, configure a unique environment name.
Deploy the Azure Cosmos DB account using azd up. The Bicep templates also deploy a sample web application.
azd up
During the provisioning process, select your subscription, desired location, and target resource group. Wait for the provisioning process to complete. The process can take approximately five minutes.
Once the provisioning of your Azure resources is done, a URL to the running web application is included in the output.
Deploying services (azd deploy)
(✓) Done: Deploying service web
- Endpoint: <https://[container-app-sub-domain].azurecontainerapps.io>
SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.
Use the URL in the console to navigate to your web application in the browser. Observe the output of the running app.
Install the client library
The client library is available through npm, as the mongodb package.
Open a terminal and navigate to the /src/ts folder.
cd ./src/ts
If not already installed, install the mongodb package using npm install.
npm install --save mongodb
Open and review the src/ts/package.json file to validate that the mongodb entry exists.
Open a terminal and navigate to the /src/js folder.
cd ./src/js
If not already installed, install the mongodb package using npm install.
npm install --save mongodb
Open and review the src/js/package.json file to validate that the mongodb entry exists.
The sample code in the template uses a database named cosmicworks and collection named products. The products collection contains details such as name, category, quantity, and a unique identifier for each product. The collection uses the /category property as a shard key.
Authenticate the client
This sample creates a new instance of the MongoClient type.
const connectionString = "<azure-cosmos-db-for-mongodb-connection-string>";
const client = new MongoClient(connectionString);
const connectionString = "<azure-cosmos-db-for-mongodb-connection-string>";
const client = new MongoClient(connectionString);
Get a database
This sample creates an instance of the Db type using the db function of the MongoClient type.
const database: Db = client.db("<database-name>");
const database = client.db("<database-name>");
Get a collection
This sample creates an instance of the Collection type using the collection function of the Db type.
This function has a generic parameter that uses the Product type defined in an interface.
Perform a point read operation by using both the unique identifier (id) and shard key fields. Use collection.findOne to efficiently retrieve the specific item.
var query: Filter<Product> = {
_id: 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb',
category: 'gear-surf-surfboards'
};
var response: WithId<Product> | null = await collection.findOne(query);
var query = {
_id: 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb',
category: 'gear-surf-surfboards'
};
var response = await collection.findOne(query);
Query documents
Perform a query over multiple items in a container using collection.find. This query finds all items within a specified category (shard key).
var query: Filter<Product> = {
category: 'gear-surf-surfboards'
};
var response: FindCursor<WithId<Product>> = await collection.find(query);
for await (const item of response) {
// Do something with each item
}
var query = {
category: 'gear-surf-surfboards'
};
var response = await collection.find(query);
for await (const item of response) {
// Do something with each item
}
Explore your data
Use the Visual Studio Code extension for Azure Cosmos DB to explore your MongoDB data. You can perform core database operations including, but not limited to:
Performing queries using a scrapbook or the query editor
Modifying, updating, creating, and deleting documents