Skip to content

Commit 7629835

Browse files
committed
Updating the Mongoose article with DB level throughput
1 parent 0cae8a0 commit 7629835

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed
8.08 KB
Loading

articles/cosmos-db/mongodb-mongoose.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ Let's create a Cosmos account. If you already have an account you want to use, y
3030

3131
[!INCLUDE [cosmos-db-create-dbaccount-mongodb](../../includes/cosmos-db-create-dbaccount-mongodb.md)]
3232

33+
### Create a database
34+
In this application we will cover two ways of creating your collections on Cosmos DB:
35+
- **Storing each object model in a separate collection**: We recommend [creating a database with dedicated throughput](set-throughput.md#set-throughput-on-a-database). Using this capacity model will give you better cost efficiency.
36+
![Node.js tutorial - Screenshot of the Azure portal, showing how to create a database in the Data Explorer for an Azure Cosmos DB account, for use with the Mongoose Node module][db-level-throughput.png]
37+
- **Storing all object models in a single Cosmos DB collection**: If you'd prefer to store all models in a single collection, you can just create a new database without selecting the Provision Throughput option. Using this capacity model will create each collection with its own throughput capacity for every object model.
38+
39+
After you create the database, you'll use the name in the `COSMOSDB_DBNAME` environment variable below.
40+
3341
## Set up your Node.js application
3442

3543
>[!Note]
@@ -41,8 +49,8 @@ Let's create a Cosmos account. If you already have an account you want to use, y
4149

4250
Answer the questions and your project will be ready to go.
4351

44-
1. Add a new file to the folder and name it ```index.js```.
45-
1. Install the necessary packages using one of the ```npm install``` options:
52+
2. Add a new file to the folder and name it ```index.js```.
53+
3. Install the necessary packages using one of the ```npm install``` options:
4654
* Mongoose: ```npm install mongoose@5 --save```
4755

4856
> [!Note]
@@ -53,26 +61,26 @@ Let's create a Cosmos account. If you already have an account you want to use, y
5361
>[!Note]
5462
> The ```--save``` flag adds the dependency to the package.json file.
5563
56-
1. Import the dependencies in your index.js file.
64+
4. Import the dependencies in your index.js file.
5765

5866
```JavaScript
5967
var mongoose = require('mongoose');
6068
var env = require('dotenv').config(); //Use the .env file to load the variables
6169
```
6270

63-
1. Add your Cosmos DB connection string and Cosmos DB Name to the ```.env``` file. Replace the placeholders {cosmos-account-name} and {dbname} with your own Cosmos account name and database name, without the brace symbols.
71+
5. Add your Cosmos DB connection string and Cosmos DB Name to the ```.env``` file. Replace the placeholders {cosmos-account-name} and {dbname} with your own Cosmos account name and database name, without the brace symbols.
6472

6573
```JavaScript
6674
# You can get the following connection details from the Azure portal. You can find the details on the Connection string pane of your Azure Cosmos account.
6775
68-
COSMODDB_USER = "<Azure Cosmos account's user name>"
69-
COSMOSDB_PASSWORD = "<Azure Cosmos account passowrd>"
76+
COSMODDB_USER = "<Azure Cosmos account's user name, usually the database account name>"
77+
COSMOSDB_PASSWORD = "<Azure Cosmos account password, this is one of the keys specified in your account>"
7078
COSMOSDB_DBNAME = "<Azure Cosmos database name>"
7179
COSMOSDB_HOST= "<Azure Cosmos Host name>"
7280
COSMOSDB_PORT=10255
7381
```
7482

75-
1. Connect to Cosmos DB using the Mongoose framework by adding the following code to the end of index.js.
83+
6. Connect to Cosmos DB using the Mongoose framework by adding the following code to the end of index.js.
7684
```JavaScript
7785
mongoose.connect("mongodb://"+process.env.COSMOSDB_HOST+":"+process.env.COSMOSDB_PORT+"/"+process.env.COSMOSDB_DBNAME+"?ssl=true&replicaSet=globaldb", {
7886
auth: {
@@ -88,19 +96,15 @@ Let's create a Cosmos account. If you already have an account you want to use, y
8896

8997
Once you are connected to Azure Cosmos DB, you can now start setting up object models in Mongoose.
9098

91-
## Caveats to using Mongoose with Cosmos DB
92-
93-
For every model you create, Mongoose creates a new collection. However, given the per-collection billing model of Cosmos DB, it might not be the most cost-efficient way to go, if you've got multiple object models that are sparsely populated.
94-
95-
This walkthrough covers both models. We'll first cover the walkthrough on storing one type of data per collection. This is the defacto behavior for Mongoose.
99+
## Best practices for using Mongoose with Cosmos DB
96100

97-
Mongoose also has a concept called [Discriminators](https://mongoosejs.com/docs/discriminators.html). Discriminators are a schema inheritance mechanism. They enable you to have multiple models with overlapping schemas on top of the same underlying MongoDB collection.
101+
For every model you create, Mongoose creates a new collection. This is best addressed using the [Database Level Throughput option](https://docs.microsoft.com/en-us/azure/cosmos-db/set-throughput#set-throughput-on-a-database), which was previously discussed. To use a single collection, you need to use Mongoose [Discriminators](https://mongoosejs.com/docs/discriminators.html). Discriminators are a schema inheritance mechanism. They enable you to have multiple models with overlapping schemas on top of the same underlying MongoDB collection.
98102

99-
You can store the various data models in the same collection and then use a filter clause at query time to pull down only the data needed.
103+
You can store the various data models in the same collection and then use a filter clause at query time to pull down only the data needed. Let's walk through each of the models.
100104
101105
### One collection per object model
102106
103-
The default Mongoose behavior is to create a MongoDB collection every time you create an Object model. This section explores how to achieve this with Azure Cosmos DB's API for MongoDB. This method is recommended when you have object models with large amounts of data. This is the default operating model for Mongoose, so, you might be familiar with this, if you're familiar with Mongoose.
107+
This section explores how to achieve this with Azure Cosmos DB's API for MongoDB. This method is our recommended approach since it allows to control cost and capacity regardless of the amount of object models in your data. This is the default operating model for Mongoose, so, you might be familiar with this, if you're familiar with Mongoose.
104108
105109
1. Open your ```index.js``` again.
106110

0 commit comments

Comments
 (0)