Skip to content

Commit 4df628e

Browse files
authored
Merge pull request #224969 from gahl-levy/mongo-basics-docs
New docs for Mongo basics
2 parents 966bf6f + 163ee8f commit 4df628e

File tree

5 files changed

+317
-0
lines changed

5 files changed

+317
-0
lines changed

articles/cosmos-db/mongodb/TOC.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858
href: tutorial-query.md
5959
- name: 3 - Distribute data globally
6060
href: tutorial-global-distribution.md
61+
- name: Aggregation pipeline
62+
href: tutorial-aggregation.md
63+
- name: Inserting data
64+
href: tutorial-insert.md
65+
- name: Updating data
66+
href: tutorial-update.md
67+
- name: Deleting data
68+
href: tutorial-delete.md
6169
- name: Create Jupyter notebooks
6270
href: ../nosql/tutorial-create-notebook.md
6371
- name: Samples
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
title: Getting Started with Aggregation Pipeline
3+
description: Learn how to get started with Cosmos DB for MongoDB aggregation pipeline for advanced data analysis and manipulation.
4+
author: gahl-levy
5+
ms.author: gahllevy
6+
ms.service: cosmos-db
7+
ms.subservice: mongodb
8+
ms.topic: tutorial
9+
ms.date: 01/24/2023
10+
ms.reviewer: mjbrown
11+
---
12+
13+
# Getting Started with Aggregation Pipeline
14+
15+
The aggregation pipeline is a powerful tool that allows developers to perform advanced data analysis and manipulation on their collections. The pipeline is a sequence of data processing operations, which are performed on the input documents to produce a computed output. The pipeline stages process the input documents and pass the result to the next stage. Each stage performs a specific operation on the data, such as filtering, grouping, sorting, and transforming.
16+
17+
## Basic Syntax
18+
19+
The basic syntax for an aggregation pipeline is as follows:
20+
21+
```javascript
22+
db.collection.aggregate([ { stage1 }, { stage2 }, ... { stageN }])
23+
```
24+
25+
Where db.collection is the MongoDB collection you want to perform the aggregation on, and stage1, stage2, ..., stageN are the pipeline stages you want to apply.
26+
27+
## Sample Stages
28+
29+
Cosmos DB for MongoDB provides a wide range of stages that you can use in your pipeline, including:
30+
31+
* $match: Filters the documents to pass only the documents that match the specified condition.
32+
* $project: Transforms the documents to a new form by adding, removing, or updating fields.
33+
* $group: Groups documents by one or more fields and performs various aggregate functions on the grouped data.
34+
* $sort: Sorts the documents based on the specified fields.
35+
* $skip: Skips the specified number of documents.
36+
* $limit: Limits the number of documents passed to the next stage.
37+
* $unwind: Deconstructs an array field from the input documents to output a document for each element.
38+
39+
To view all available stages, see [supported features](feature-support-42.md)
40+
41+
## Examples
42+
43+
Here are some examples of how you can use the aggregation pipeline to perform various operations on your data:
44+
45+
Filtering: To filter documents that have a "quantity" field greater than 20, you can use the following pipeline:
46+
```javascript
47+
db.collection.aggregate([
48+
{ $match: { quantity: { $gt: 20 } } }
49+
])
50+
```
51+
52+
Grouping: To group documents by the "category" field and calculate the total "quantity" for each group, you can use the following pipeline:
53+
```javascript
54+
db.collection.aggregate([
55+
{ $group: { _id: "$category", totalQuantity: { $sum: "$quantity" } } }
56+
])
57+
```
58+
59+
Sorting: To sort documents by the "price" field in descending order, you can use the following pipeline:
60+
```javascript
61+
db.collection.aggregate([
62+
{ $sort: { price: -1 } }
63+
])
64+
```
65+
66+
Transforming: To add a new field "discount" to documents that have a "price" greater than 100, you can use the following pipeline:
67+
68+
```javascript
69+
db.collection.aggregate([
70+
{ $project: { item: 1, price: 1, discount: { $cond: [{ $gt: ["$price", 100] }, 10, 0 ] } } }
71+
])
72+
```
73+
74+
Unwinding: To separate all the subdocuments from the array field 'tags' and create a new document for each value, you can use the following pipeline:
75+
```javascript
76+
db.collection.aggregate([
77+
{ $unwind: "$tags" }
78+
])
79+
```
80+
81+
## Example with multiple stages
82+
83+
```javascript
84+
db.sales.aggregate([
85+
{ $match: { date: { $gte: "2021-01-01", $lt: "2021-03-01" } } },
86+
{ $group: { _id: "$category", totalSales: { $sum: "$sales" } } },
87+
{ $sort: { totalSales: -1 } },
88+
{ $limit: 5 }
89+
])
90+
```
91+
92+
In this example, we are using a sample collection called "sales" which has documents with the following fields: "date", "category", and "sales".
93+
94+
The first stage { $match: { date: { $gte: "2021-01-01", $lt: "2021-03-01" } } } filters the documents by the "date" field, only passing documents with a date between January 1st, 2021 and February 28th, 2021. We are using a string date format with the format "YYYY-MM-DD".
95+
96+
The second stage { $group: { _id: "$category", totalSales: { $sum: "$sales" } } } groups the documents by the "category" field and calculates the total sales for each group.
97+
98+
The third stage { $sort: { totalSales: -1 } } sorts the documents by the "totalSales" field in descending order.
99+
100+
The fourth stage { $limit: 5 } limits the number of documents passed to the next stage to only the top 5.
101+
102+
As a result, the pipeline will return the top 5 categories by total sales for the specified date range.
103+
104+
## Next steps
105+
106+
- Learn how to [use Studio 3T](connect-using-mongochef.md) with Azure Cosmos DB for MongoDB.
107+
- Learn how to [use Robo 3T](connect-using-robomongo.md) with Azure Cosmos DB for MongoDB.
108+
- Explore MongoDB [samples](nodejs-console-app.md) with Azure Cosmos DB for MongoDB.
109+
- Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
110+
- If all you know is the number of vCores and servers in your existing database cluster, read about [estimating request units using vCores or vCPUs](../convert-vcore-to-request-unit.md).
111+
- If you know typical request rates for your current database workload, read about [estimating request units using Azure Cosmos DB capacity planner](estimate-ru-capacity-planner.md).
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: Deleting Data into Cosmos DB for MongoDB
3+
description: Learn how to get started with deleting data in Cosmos DB for MongoDB.
4+
author: gahl-levy
5+
ms.author: gahllevy
6+
ms.service: cosmos-db
7+
ms.subservice: mongodb
8+
ms.topic: tutorial
9+
ms.date: 01/24/2023
10+
ms.reviewer: mjbrown
11+
---
12+
13+
# Deleting Data into Cosmos DB for MongoDB
14+
15+
One of the most basic operations is deleting data in a collection. In this guide, we will cover everything you need to know about deleting data using the Mongo Shell (Mongosh).
16+
17+
## Understanding the deleteOne() and deleteMany() Methods
18+
19+
The most common way to delete data in MongoDB is to delete individual documents from a collection. You can do this using the deleteOne() or deleteMany() method.
20+
21+
The deleteOne() method is used to delete a single document from a collection that matches a specific filter. For example, if you wanted to delete a user with the name "John Doe" from the "users" collection, you would use the following command:
22+
23+
```javascript
24+
db.users.deleteOne({ "name": "John Doe" })
25+
```
26+
27+
The deleteMany() method, on the other hand, is used to delete multiple documents from a collection that match a specific filter. For example, if you wanted to delete all users with an age less than 30 from the "users" collection, you would use the following command:
28+
29+
```javascript
30+
db.users.deleteMany({ "age": { $lt: 30 } })
31+
```
32+
33+
It's important to note that both of these methods return an object with the following properties:
34+
35+
deletedCount: The number of documents deleted.
36+
acknowledged: This property will be true.
37+
38+
## Deleting a Collection
39+
40+
To delete an entire collection, use the drop() method. For example, if you wanted to delete the "users" collection, you would use the following command:
41+
42+
```javascript
43+
db.users.drop()
44+
This will delete the "users" collection and all of its documents permanently.
45+
```
46+
47+
## Next steps
48+
49+
- Learn how to [use Studio 3T](connect-using-mongochef.md) with Azure Cosmos DB for MongoDB.
50+
- Learn how to [use Robo 3T](connect-using-robomongo.md) with Azure Cosmos DB for MongoDB.
51+
- Explore MongoDB [samples](nodejs-console-app.md) with Azure Cosmos DB for MongoDB.
52+
- Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
53+
- If all you know is the number of vCores and servers in your existing database cluster, read about [estimating request units using vCores or vCPUs](../convert-vcore-to-request-unit.md).
54+
- If you know typical request rates for your current database workload, read about [estimating request units using Azure Cosmos DB capacity planner](estimate-ru-capacity-planner.md).
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: Getting Started with Inserting data into Cosmos DB for MongoDB
3+
description: Learn how to get started with inserting data into Cosmos DB for MongoDB.
4+
author: gahl-levy
5+
ms.author: gahllevy
6+
ms.service: cosmos-db
7+
ms.subservice: mongodb
8+
ms.topic: tutorial
9+
ms.date: 01/24/2023
10+
ms.reviewer: mjbrown
11+
---
12+
13+
# Inserting Data into Cosmos DB for MongoDB
14+
One of the most basic operations is inserting data into a collection. In this guide, we will cover everything you need to know about inserting data using the Mongo Shell (Mongosh).
15+
16+
## Inserting a Single Document
17+
18+
The most basic way to insert data into MongoDB is to insert a single document. To do this, you can use the db.collection.insertOne() method. The insertOne() method takes a single document as its argument and inserts it into the specified collection. Here's an example of how you might use this method:
19+
20+
```javascript
21+
db.myCollection.insertOne({
22+
name: "John Smith",
23+
age: 30,
24+
address: "123 Main St"
25+
});
26+
```
27+
28+
In this example, we're inserting a document into the "myCollection" collection with the following fields: "name", "age", and "address". Once the command is executed, you will see the acknowledged: true and insertedId: ObjectId("5f5d5f5f5f5f5f5f5f5f5f5f") in the output, where the insertedId is the unique identifier generated by MongoDB for the inserted document.
29+
30+
## Inserting Multiple Documents
31+
32+
In many cases, you'll need to insert multiple documents at once. To do this, you can use the db.collection.insertMany() method. The insertMany() method takes an array of documents as its argument and inserts them into the specified collection. Here's an example:
33+
34+
```javascript
35+
db.myCollection.insertMany([
36+
{name: "Jane Doe", age: 25, address: "456 Park Ave"},
37+
{name: "Bob Smith", age: 35, address: "789 Elm St"},
38+
{name: "Sally Johnson", age: 40, address: "111 Oak St"}
39+
]);
40+
```
41+
42+
In this example, we're inserting three documents into the "myCollection" collection. Each document has the same fields as the previous example: "name", "age", and "address". The insertMany() method returns an acknowledged: true and insertedIds: [ObjectId("5f5d5f5f5f5f5f5f5f5f5f5f"), ObjectId("5f5d5f5f5f5f5f5f5f5f5f5f"), ObjectId("5f5d5f5f5f5f5f5f5f5f5f5f")] where insertedIds is an array of unique identifiers generated by MongoDB for each inserted document.
43+
44+
## Inserting with Options
45+
46+
Both insertOne() and insertMany() accept an optional second argument, which can be used to specify options for the insert operation. For example, to set the "ordered" option to false, you can use the following code:
47+
48+
```javascript
49+
db.myCollection.insertMany([
50+
{name: "Jane Doe", age: 25, address: "456 Park Ave"},
51+
{name: "Bob Smith", age: 35, address: "789 Elm St"},
52+
{name: "Sally Johnson", age: 40, address: "111 Oak St"}
53+
], {ordered: false});
54+
```
55+
56+
This tells MongoDB to insert the documents in an unordered fashion, meaning that if one document fails to be inserted, it will continue with the next one. This is recommended for write performance in Cosmos DB for MongoDB
57+
58+
## Next steps
59+
60+
- Learn how to [use Studio 3T](connect-using-mongochef.md) with Azure Cosmos DB for MongoDB.
61+
- Learn how to [use Robo 3T](connect-using-robomongo.md) with Azure Cosmos DB for MongoDB.
62+
- Explore MongoDB [samples](nodejs-console-app.md) with Azure Cosmos DB for MongoDB.
63+
- Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
64+
- If all you know is the number of vCores and servers in your existing database cluster, read about [estimating request units using vCores or vCPUs](../convert-vcore-to-request-unit.md).
65+
- If you know typical request rates for your current database workload, read about [estimating request units using Azure Cosmos DB capacity planner](estimate-ru-capacity-planner.md).
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: Updating Data into Cosmos DB for MongoDB
3+
description: Learn how to get started with updating data in Cosmos DB for MongoDB.
4+
author: gahl-levy
5+
ms.author: gahllevy
6+
ms.service: cosmos-db
7+
ms.subservice: mongodb
8+
ms.topic: tutorial
9+
ms.date: 01/24/2023
10+
ms.reviewer: mjbrown
11+
---
12+
13+
# Updating Data into Cosmos DB for MongoDB
14+
15+
One of the most basic operations is updating data into a collection. In this guide, we will cover everything you need to know about updating data using the Mongo Shell (Mongosh).
16+
17+
## Using updateOne() Method
18+
19+
The updateOne() method updates the first document that matches a specified filter. The method takes two parameters:
20+
21+
filter: A document that specifies the criteria for the update. The filter is used to match the documents in the collection that should be updated. The filter document must be a valid query document.
22+
23+
update: A document that specifies the update operations to perform on the matching documents. The update document must be a valid update document.
24+
25+
```javascript
26+
db.collection.updateOne(
27+
<filter>,
28+
<update>
29+
)
30+
```
31+
32+
For example, to update the name of a customer with _id equal to 1, you can use the following command:
33+
34+
```javascript
35+
db.customers.updateOne(
36+
{ _id: 1 },
37+
{ $set: { name: "Jane Smith" } }
38+
)
39+
```
40+
41+
In the above example, db.customers is the collection name, { _id: 1 } is the filter which matches the first document that has _id equal to 1 and { $set: { name: "Jane Smith" } } is the update operation which sets the name field of the matched document to "Jane Smith".
42+
43+
You can also use other update operators like $inc, $mul, $rename, $unset etc. to update the data.
44+
45+
## updateMany() Method
46+
47+
The updateMany() method updates all documents that match a specified filter. The method takes two parameters:
48+
49+
filter: A document that specifies the criteria for the update. The filter is used to match the documents in the collection that should be updated. The filter document must be a valid query document.
50+
update: A document that specifies the update operations to perform on the matching documents. The update document must be a valid update document.
51+
52+
```javascript
53+
db.collection.updateMany(
54+
<filter>,
55+
<update>
56+
)
57+
```
58+
59+
For example, to update the name of all customers that live in "New York", you can use the following command:
60+
61+
```javascript
62+
db.customers.updateMany(
63+
{ city: "New York" },
64+
{ $set: { name: "Jane Smith" } }
65+
)
66+
```
67+
68+
In the above example, db.customers is the collection name, { city: "New York" } is the filter which matches all the documents that have city field equal to "New York" and { $set: { name: "Jane Smith" } } is the update operation which sets the name field of all the matched documents to "Jane Smith".
69+
70+
You can also use other update operators like $inc, $mul, $rename, $unset, etc. to update the data.
71+
72+
## Next steps
73+
74+
- Learn how to [use Studio 3T](connect-using-mongochef.md) with Azure Cosmos DB for MongoDB.
75+
- Learn how to [use Robo 3T](connect-using-robomongo.md) with Azure Cosmos DB for MongoDB.
76+
- Explore MongoDB [samples](nodejs-console-app.md) with Azure Cosmos DB for MongoDB.
77+
- Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
78+
- If all you know is the number of vCores and servers in your existing database cluster, read about [estimating request units using vCores or vCPUs](../convert-vcore-to-request-unit.md).
79+
- If you know typical request rates for your current database workload, read about [estimating request units using Azure Cosmos DB capacity planner](estimate-ru-capacity-planner.md).

0 commit comments

Comments
 (0)