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
Copy file name to clipboardExpand all lines: articles/cosmos-db/mongodb-custom-commands.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,13 +10,13 @@ ms.author: sngun
10
10
11
11
# Use MongoDB extension commands to manage data stored in Azure Cosmos DB’s API for MongoDB
12
12
13
-
Azure Cosmos DB is Microsoft's globally distributed multi-model database service. You can communicate with the Azure Cosmos DB’s API for MongoDB by using any of the opensource [MongoDB client drivers](https://docs.mongodb.org/ecosystem/drivers). The Azure Cosmos DB’s API for MongoDB enables the use of existing client drivers by adhering to the [MongoDB wire protocol](https://docs.mongodb.org/manual/reference/mongodb-wire-protocol).
13
+
Azure Cosmos DB is Microsoft's globally distributed multi-model database service. You can communicate with the Azure Cosmos DB’s API for MongoDB by using any of the open-source [MongoDB client drivers](https://docs.mongodb.org/ecosystem/drivers). The Azure Cosmos DB’s API for MongoDB enables the use of existing client drivers by adhering to the [MongoDB wire protocol](https://docs.mongodb.org/manual/reference/mongodb-wire-protocol).
14
14
15
15
By using the Azure Cosmos DB’s API for MongoDB, you can enjoy the benefits Cosmos DB such as global distribution, automatic sharding, high availability, latency guarantees, automatic, encryption at rest, backups, and many more, while preserving your investments in your MongoDB app.
16
16
17
17
## MongoDB protocol support
18
18
19
-
By default, the Azure Cosmos DB’s API for MongoDB is compatible with MongoDB server version 3.2, for more details, see [supported features and syntax](mongodb-feature-support.md). The features or query operators added in MongoDB version 3.4 are currently available as a preview in the Azure Cosmos DB’s API for MongoDB. The following extension commands support Azure Cosmos DB specific functionality when performing CRUD operations on the data stored in Azure Cosmos DB’s API for MongoDB:
19
+
By default, the Azure Cosmos DB’s API for MongoDB is compatible with MongoDB server version 3.2, for more details, see [supported features and syntax](mongodb-feature-support.md). The features or query operators added in MongoDB version 3.4 are currently available as a preview in the Azure Cosmos DB’s API for MongoDB. The following extension commands support specific Azure Cosmos DB functionality when performing CRUD operations on the data stored in Azure Cosmos DB’s API for MongoDB:
20
20
21
21
*[Create database](#create-database)
22
22
*[Update database](#update-database)
@@ -155,12 +155,12 @@ The create collection extension command creates a new MongoDB collection. The da
155
155
156
156
The following table describes the parameters within the command:
157
157
158
-
|**Field**|**Type**|**Description**|
159
-
|---------|---------|---------|
160
-
| customAction | string| Name of the custom command. Must be "CreateCollection"|
161
-
| collection | string| Name of the collection|
162
-
| offerThroughput | int | Provisioned Throughput to set on the database. It's an Optional parameter |
163
-
| shardKey | string |Shard Key path to create a sharded collection. It's an Optional parameter|
158
+
|**Field**|**Type**|**Required**|**Description**|
159
+
|---------|---------|---------|---------|
160
+
| customAction | string | Required| Name of the custom command. Must be "CreateCollection".|
161
+
| collection | string | Required| Name of the collection. No special characters are allowed.|
162
+
| offerThroughput | int | Optional*| Provisioned throughput to set on the database. If this parameter is not provided, it will default to the minimum, 400 RU/s. * To specify throughput beyond 10,000 RU/s, the `shardKey`parameter is required.|
163
+
| shardKey | string | Optional*| The path to the Shard Key for the sharded collection. This parameter is required if you set more than 10,000 RU/s in `offerThroughput`. If it is specified, all documents inserted will require this value.|
164
164
165
165
### Output
166
166
@@ -179,7 +179,7 @@ db.runCommand({customAction: "CreateCollection", collection: "testCollection", o
179
179
180
180
**Create a sharded collection**
181
181
182
-
To create a sharded collection with name "testCollection" and provisioned throughput of 1000 RUs, use the following command:
182
+
To create a sharded collection with name "testCollection" and provisioned throughput of 1000 RUs, and a shardkey property "a.b", use the following command:
In this application we will cover two ways of creating collections in Azure 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
+
37
+
:::image type="content" source="./media/mongodb-mongoose/db-level-throughput.png" alt-text="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":::
38
+
39
+
-**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.
40
+
41
+
After you create the database, you'll use the name in the `COSMOSDB_DBNAME` environment variable below.
42
+
33
43
## Set up your Node.js application
34
44
35
45
>[!Note]
@@ -41,8 +51,8 @@ Let's create a Cosmos account. If you already have an account you want to use, y
41
51
42
52
Answer the questions and your project will be ready to go.
43
53
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:
54
+
2. Add a new file to the folder and name it ```index.js```.
55
+
3. Install the necessary packages using one of the ```npm install``` options:
46
56
* Mongoose: ```npm install mongoose@5 --save```
47
57
48
58
> [!Note]
@@ -53,26 +63,26 @@ Let's create a Cosmos account. If you already have an account you want to use, y
53
63
>[!Note]
54
64
> The ```--save``` flag adds the dependency to the package.json file.
55
65
56
-
1. Import the dependencies in your index.js file.
66
+
4. Import the dependencies in your index.js file.
57
67
58
68
```JavaScript
59
69
var mongoose =require('mongoose');
60
70
var env =require('dotenv').config(); //Use the .env file to load the variables
61
71
```
62
72
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.
73
+
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
74
65
75
```JavaScript
66
76
# 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
77
68
-
COSMODDB_USER = "<Azure Cosmos account's user name>"
@@ -88,19 +98,15 @@ Let's create a Cosmos account. If you already have an account you want to use, y
88
98
89
99
Once you are connected to Azure Cosmos DB, you can now start setting up object models in Mongoose.
90
100
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.
101
+
## Best practices for using Mongoose with Cosmos DB
96
102
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.
103
+
For every model you create, Mongoose creates a newcollection. This is best addressed using the [Database Level Throughput option](set-throughput.md#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
104
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.
105
+
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
106
101
107
### One collection per object model
102
108
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.
109
+
This section explores how to achieve this with Azure Cosmos DB's APIforMongoDB. This method is our recommended approach since it allows you to control cost and capacity. As a result, the amount ofRequest Units on the database does not depend on the number of object models. This is the default operating model for Mongoose, so, you might be familiar withthis.
104
110
105
111
1. Open your ```index.js``` again.
106
112
@@ -313,3 +319,4 @@ As you can see, it is easy to work with Mongoose discriminators. So, if you have
0 commit comments