Skip to content
Azure examples for Cosmos DB with Node.js and mongoose incl. cost optimization
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Azure Examples: Cosmos DB and Mongoose

Build Status

This repository shows how to use Cosmos DB with Node.js and the MongoDB protocol with the popular mongoose npm package.

This is the goal result:

$ npm run simple-examples

> node examples/simple/run.js

[INFO] Connected (…)
[INFO] Number of Families: 4
[INFO] New Family saved (…)
[INFO] Number of Families: 5
[INFO] Disconnected, bye bye

This repository guides you through the examples. It is recommended to browse through on before trying out the code.

Note: some code is from the official azure docs. It was fixed, updated for 2019 and adds CI to ensure the example remains working.

Step 1 - Create a Cosmos DB in Azure Portal

  1. Login to →

  2. Follow the the official Azure Docs: Connect a Node.js Mongoose application to Azure Cosmos DB to create a cosmos account. Then come back here. Do not follow the Node.js code, which is outdated.

  3. Enable Aggregation Pipeline in your CosmosDB per to use mongoose's rich API. Otherwise some operations like mongoose's db.collection.countDocuments() will not work.

  4. Take note of your credentials in the Connect String plane. You will need these later.

    Cosmos DB Credentials

Step 2 - Get Code

First you need the code in this repository

git clone

Step 3 - Configure Credentials

To test your connection locally, copy .env.example and rename it to .env filling in your credentials:

Variable Example Reference in Azure Portal
DB_HOST "Host"
DB_USER exampledb "Username"
DB_PASSWORD "Primary Password"
DB_PORT 10255 "Port". Default is 10255
DB_NAME testdb You must first create this in the "Data Explorer" plane of your Cosmos DB.

Last Step - run the examples

Finally, install the dependencies and run all examples:

npm install
npm run simple-examples
npm run optimized-examples

Azure Cost Optimization

By default Mongoose creates a new colleciton per model. Because Azure charges you per collection, it makes sense to use mongoose discriminators to leverage schema inheritance to optimize costs:

Then you only pay once for the Base model collection instaead of doubling costs for Family and Vacation models.

For more details see the cost optimized examples →


New to JavaScript?

Don't worry. These examples are easy to follow. But just in case, you need some guidance:

  • Promises and Async/Await
    To run multiple examples with a single connection, we have to add some extra Promises. See details at ./examples/

  • ES6 Modules
    Compared to the official Microsoft documentation, each operation is split into its own file, e.g. family.add.js.

    Learn about ES6 modules here on MSN →

Have fun exploring the examples. If you have any questions, open an issue →

You can’t perform that action at this time.