Azure Cosmos DB Node.js examples

Tip

The Azure Cosmos DB DocumentDB API or SQL (DocumentDB) API is now known as Azure Cosmos DB SQL API. You don't need to change anything to continue running your apps built with DocumentDB API. The functionality remains the same.

Sample solutions that perform CRUD operations and other common operations on Azure Cosmos DB resources are included in the azure-documentdb-nodejs 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.

Alternatively, 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 for this tutorial with a URI of https://localhost:8081 and a key of

C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==

You also need the Node.js SDK.

Note

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

Database examples

The app.js file of the DatabaseManagement project shows how to perform the following tasks.

Task API reference
Create a database DocumentClient.createDatabase
Query an account for a database DocumentClient.queryDatabases
Read a database by Id DocumentClient.readDatabase
List databases for an account DocumentClient.readDatabases
Delete a database DocumentClient.deleteDatabase

Collection examples

The app.js file of the CollectionManagement project shows how to perform the following tasks.

Task API reference
Create a collection DocumentClient.createCollection
Read a list of all collections in a database DocumentClient.readCollections
Get a collection by _self DocumentClient.readCollection
Get a collection by Id DocumentClient.readCollection
Get performance tier of a collection DocumentQueryable.queryOffers
Change performance tier of a collection DocumentClient.replaceOffer
Delete a collection DocumentClient.deleteCollection

Document examples

The app.js file of the DocumentManagement project shows how to perform the following tasks.

Task API reference
Create documents DocumentClient.createDocument
Read the document feed for a collection DocumentClient.readDocument
Read a document by ID DocumentClient.readDocument
Read document only if document has changed DocumentClient.readDocument
RequestOptions.accessCondition
Query for documents DocumentClient.queryDocuments
Replace a document DocumentClient.replaceDocument
Replace document with conditional ETag check DocumentClient.replaceDocument
RequestOptions.accessCondition
Delete a document DocumentClient.deleteDocument

Indexing examples

The app.js file of the IndexManagement project shows how to perform the following tasks.

Task API reference
Create a collection with default indexing DocumentClient.createCollection
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 collections IndexingMode.Lazy
Include specific paths of a document in indexing IndexingPolicy.IncludedPaths
Exclude certain paths from indexing 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, DocumentClient.queryDocument
Create a collection with default indexPolicy, then update this online DocumentClient.createCollection
DocumentClient.replaceCollection#replaceCollection

For more information about indexing, see Azure Cosmos DB indexing policies.

Server-side programming examples

The app.js file of the ServerSideScripts project shows how to perform the following tasks.

Task API reference
Create a stored procedure DocumentClient.createStoredProcedure
Execute a stored procedure DocumentClient.executeStoredProcedure

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

Partitioning examples

The app.js file of the Partitioning project shows how to perform the following tasks.

Task API reference
Use a HashPartitionResolver HashPartitionResolver

For more information about partitioning data in Azure Cosmos DB, see Partition and scale data in Azure Cosmos DB.