Skip to content

Commit 01b58a2

Browse files
authored
Merge pull request #109772 from LuisBosquez/master
Mongo extension commands and Mongoose article
2 parents 7034200 + 52fcfd0 commit 01b58a2

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed
8.08 KB
Loading

articles/cosmos-db/mongodb-custom-commands.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ ms.author: sngun
1010

1111
# Use MongoDB extension commands to manage data stored in Azure Cosmos DB’s API for MongoDB
1212

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).
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).
1414

1515
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.
1616

1717
## MongoDB protocol support
1818

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:
2020

2121
* [Create database](#create-database)
2222
* [Update database](#update-database)
@@ -155,12 +155,12 @@ The create collection extension command creates a new MongoDB collection. The da
155155

156156
The following table describes the parameters within the command:
157157

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. |
164164

165165
### Output
166166

@@ -179,7 +179,7 @@ db.runCommand({customAction: "CreateCollection", collection: "testCollection", o
179179

180180
**Create a sharded collection**
181181

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:
183183

184184
```shell
185185
use test

articles/cosmos-db/mongodb-mongoose.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ 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 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+
3343
## Set up your Node.js application
3444

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

4252
Answer the questions and your project will be ready to go.
4353

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:
4656
* Mongoose: ```npm install mongoose@5 --save```
4757

4858
> [!Note]
@@ -53,26 +63,26 @@ Let's create a Cosmos account. If you already have an account you want to use, y
5363
>[!Note]
5464
> The ```--save``` flag adds the dependency to the package.json file.
5565
56-
1. Import the dependencies in your index.js file.
66+
4. Import the dependencies in your index.js file.
5767

5868
```JavaScript
5969
var mongoose = require('mongoose');
6070
var env = require('dotenv').config(); //Use the .env file to load the variables
6171
```
6272

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.
6474

6575
```JavaScript
6676
# 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.
6777
68-
COSMODDB_USER = "<Azure Cosmos account's user name>"
69-
COSMOSDB_PASSWORD = "<Azure Cosmos account passowrd>"
78+
COSMODDB_USER = "<Azure Cosmos account's user name, usually the database account name>"
79+
COSMOSDB_PASSWORD = "<Azure Cosmos account password, this is one of the keys specified in your account>"
7080
COSMOSDB_DBNAME = "<Azure Cosmos database name>"
7181
COSMOSDB_HOST= "<Azure Cosmos Host name>"
7282
COSMOSDB_PORT=10255
7383
```
7484

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

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

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.
101+
## Best practices for using Mongoose with Cosmos DB
96102

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 new collection. 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.
98104

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.
100106
101107
### One collection per object model
102108
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.
109+
This section explores how to achieve this with Azure Cosmos DB's API for MongoDB. This method is our recommended approach since it allows you to control cost and capacity. As a result, the amount of Request 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 with this.
104110

105111
1. Open your ```index.js``` again.
106112

@@ -313,3 +319,4 @@ As you can see, it is easy to work with Mongoose discriminators. So, if you have
313319
314320
[alldata]: ./media/mongodb-mongoose/mongo-collections-alldata.png
315321
[multiple-coll]: ./media/mongodb-mongoose/mongo-mutliple-collections.png
322+
[dbleveltp]: ./media/mongodb-mongoose/db-level-throughput.png

0 commit comments

Comments
 (0)