You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
33
41
## Set up your Node.js application
34
42
35
43
>[!Note]
@@ -41,8 +49,8 @@ Let's create a Cosmos account. If you already have an account you want to use, y
41
49
42
50
Answer the questions and your project will be ready to go.
43
51
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:
46
54
* Mongoose: ```npm install mongoose@5 --save```
47
55
48
56
> [!Note]
@@ -53,26 +61,26 @@ Let's create a Cosmos account. If you already have an account you want to use, y
53
61
>[!Note]
54
62
> The ```--save``` flag adds the dependency to the package.json file.
55
63
56
-
1. Import the dependencies in your index.js file.
64
+
4. Import the dependencies in your index.js file.
57
65
58
66
```JavaScript
59
67
var mongoose =require('mongoose');
60
68
var env =require('dotenv').config(); //Use the .env file to load the variables
61
69
```
62
70
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.
64
72
65
73
```JavaScript
66
74
# 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.
67
75
68
-
COSMODDB_USER = "<Azure Cosmos account's user name>"
@@ -88,19 +96,15 @@ Let's create a Cosmos account. If you already have an account you want to use, y
88
96
89
97
Once you are connected to Azure Cosmos DB, you can now start setting up object models in Mongoose.
90
98
91
-
## Caveats to using Mongoose with Cosmos DB
92
-
93
-
For every model you create, Mongoose creates a newcollection. 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
96
100
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 newcollection. 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.
98
102
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.
100
104
101
105
### One collection per object model
102
106
103
-
The default Mongoose behavior is to create a MongoDB collection every time you create an Objectmodel. This section explores how to achieve thiswith 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 APIforMongoDB. This method is our recommended approach since it allows to control cost and capacity regardless of the amount of object models in yourdata. This is the default operating model for Mongoose, so, you might be familiar withthis, if you're familiar with Mongoose.
0 commit comments