Examples for Azure Cosmos DB for NoSQL SDK for JS
APPLIES TO: NoSQL
Sample solutions that perform CRUD operations and other common operations on Azure Cosmos DB resources are included in the JavaScript SDK for Azure Cosmos DB 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 trial account before you begin.
- You can activate Visual Studio subscriber benefits: Your Visual Studio subscription gives you credits every month that you can use for paid Azure services.
You can create an Azure Cosmos DB free tier account, with the first 1000 RU/s and 25 GB of storage for free. You can also use the Azure Cosmos DB emulator with a URI of https://localhost:8081
. For the key to use with the emulator, see 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 DatabaseManagement file shows how to perform the CRUD operations on the database. To learn about the Azure Cosmos DB databases before running the following samples, see Working with databases, containers, and items conceptual article.
Container examples
The ContainerManagement file shows how to perform the CRUD operations on the container. To learn about the Azure Cosmos DB collections before running the following samples, see Working with databases, containers, and items conceptual article.
Item examples
The ItemManagement file shows how to perform the CRUD operations on the item. To learn about the Azure Cosmos DB documents before running the following samples, see Working with databases, containers, and items conceptual article.
Indexing examples
The IndexManagement file shows how to manage indexing. To learn about indexing in Azure Cosmos DB before running the following samples, see indexing policies, indexing types, and indexing paths conceptual articles.
Server-side programming examples
The index.ts file 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 |
Bulk update with stored procedure | StoredProcedure.execute |
For more information about server-side programming, see Azure Cosmos DB server-side programming: Stored procedures, database triggers, and UDFs.
Microsoft Entra Auth Examples
The EntraAuth.ts file shows how to perform the following tasks.
Task | API reference |
---|---|
Create credential object from @azure/identity | API |
Pass token credentials to client constructor | API |
Execute cosmos client with aad credentials | API |
Miscellaneous samples
Following curated samples illustrate common scenarios.
Task | API reference |
---|---|
Alter Query throughput | API |
Getting query throughput | API |
using SasTokens for granting scoped access to Cosmos DB resources | API |
Next steps
Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
- If all you know is the number of vCores and servers in your existing database cluster, see estimating request units using vCores or vCPUs
- If you know typical request rates for your current database workload, read about estimating request units using Azure Cosmos DB capacity planner