Azure Cosmos DB Node.js examples

Sample solutions that perform CRUD operations and other common operations on Azure Cosmos DB resources are included in the azure-cosmos-js GitHub repository. This article provides:

  • Links to the tasks in each of the Node.js example project files.
  • Links to the related API reference content.

Prerequisites

If you don't have an Azure subscription, create a free account before you begin.

You can Try Azure Cosmos DB for free without an Azure subscription, free of charge and commitments. Or, you can use the Azure Cosmos DB Emulator with a URI of https://localhost:8081. The Primary Key is provided in Authenticating requests.

You also need the JavaScript SDK.

Note

Each sample is self-contained, it sets itself up and cleans up after itself. As such, the samples issue multiple calls to Containers.create. Each time this is done your subscription will be billed for 1 hour of usage per the performance tier of the container being created.

Database examples

The app.js file of the DatabaseManagement project shows how to perform the following tasks. To learn about the Azure Cosmos databases before running the following samples, see Working with databases, containers, and items conceptual article.

Task API reference
Create a database if it does not exist Databases.createIfNotExists
List databases for an account Databases.readAll
Read a database by Id Database.read
Delete a database Database.delete

Container examples

The app.js file of the ContainerManagement project shows how to perform the following tasks. To learn about the Azure Cosmos collections before running the following samples, see Working with databases, containers, and items conceptual article.

Task API reference
Create a container if it does not exist Containers.createIfNotExists
List containers for an account Containers.readAll
Read a collection by Id Container.read
Delete a container Container.delete

Item examples

The app.js file of the ItemManagement project shows how to perform the following tasks. To learn about the Azure Cosmos documents before running the following samples, see Working with databases, containers, and items conceptual article.

Task API reference
Create items Items.create
Read all items in a container Items.readAll
Read an item by ID Item.read
Read item only if item has changed Item.read
RequestOptions.accessCondition
Query for documents Items.query
Replace an item Item.replace
Replace item with conditional ETag check Item.replace
RequestOptions.accessCondition
Delete an item Item.delete

Indexing examples

The app.js file of the IndexManagement project shows how to perform the following tasks. To learn about indexing in Azure Cosmos DB before running the following samples, see indexing policies, indexing types, and indexing paths conceptual articles.

Task API reference
Manually index a specific document RequestOptions.indexingDirective: 'include'
Manually exclude a specific document from the index RequestOptions.indexingDirective: 'exclude'
Use lazy indexing for bulk import or read heavy containers IndexingMode.Lazy
Exclude a path from the index IndexingPolicy.ExcludedPath
Allow a scan on a string path during a range operation FeedOptions.EnableScanInQuery
Create a range index on a string path IndexKind.Range, IndexingPolicy, Items.query
Create a container with default indexPolicy, then update this online Containers.create

Server-side programming examples

The app.js file of the ServerSideScripts project shows how to perform the following tasks. To learn about Server-side programming in Azure Cosmos DB before running the following samples, see Stored procedures, triggers, and user-defined functions conceptual article.

Task API reference
Create a stored procedure StoredProcedures.create
Execute a stored procedure StoredProcedure.execute

For more information about server-side programming, see Azure Cosmos DB server-side programming: Stored procedures, database triggers, and UDFs.