Skip to content

Commit bb698d2

Browse files
authored
Merge pull request #1860 from MicrosoftDocs/main
4/7/2025 AM Publish
2 parents 56f3319 + a229052 commit bb698d2

80 files changed

Lines changed: 2313 additions & 251 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

articles/cosmos-db/mongodb/vcore/commands/aggregation/TOC.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
- name: count
44
href: count.md
55
- name: distinct
6-
href: distinct.md
6+
href: distinct.md
7+
- name: set
8+
href: set.md
9+
- name: replacewith
10+
href: replacewith.md
11+
- name: sample
12+
href: sample.md
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
---
2+
title: $replaceWith
3+
titleSuffix: Overview of the $replaceWith operator in Azure Cosmos DB for MongoDB vCore
4+
description: The $replaceWith operator in Azure Cosmos DB for MongoDB vCore returns a document after replacing a document with the specified document
5+
author: abinav2307
6+
ms.author: abramees
7+
ms.service: azure-cosmos-db
8+
ms.subservice: mongodb-vcore
9+
ms.topic: conceptual
10+
ms.date: 02/24/2025
11+
---
12+
13+
# $replaceWith
14+
15+
The `$replaceWith` aggregation stage operator is used to replace the input document with the specified document. The `$replaceWith` operator transforms documents from one structure to another or replaces them entirely with new fields and values.
16+
17+
## Syntax
18+
19+
```mongodb
20+
{
21+
"$replaceWith": <newDocument>
22+
}
23+
```
24+
25+
## Parameters
26+
27+
| Parameter | Description |
28+
| --- | --- |
29+
| **`newDocument`** | The new document to replace the original document|
30+
31+
## Examples
32+
33+
Consider this sample document from the stores collection in the StoreData database.
34+
35+
```json
36+
{
37+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
38+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
39+
"location": {
40+
"lat": -89.2384,
41+
"lon": -46.4012
42+
},
43+
"staff": {
44+
"totalStaff": {
45+
"fullTime": 8,
46+
"partTime": 20
47+
}
48+
},
49+
"sales": {
50+
"totalSales": 75670,
51+
"salesByCategory": [
52+
{
53+
"categoryName": "Wine Accessories",
54+
"totalSales": 34440
55+
},
56+
{
57+
"categoryName": "Bitters",
58+
"totalSales": 39496
59+
},
60+
{
61+
"categoryName": "Rum",
62+
"totalSales": 1734
63+
}
64+
]
65+
},
66+
"promotionEvents": [
67+
{
68+
"eventName": "Unbeatable Bargain Bash",
69+
"promotionalDates": {
70+
"startDate": {
71+
"Year": 2024,
72+
"Month": 6,
73+
"Day": 23
74+
},
75+
"endDate": {
76+
"Year": 2024,
77+
"Month": 7,
78+
"Day": 2
79+
}
80+
},
81+
"discounts": [
82+
{
83+
"categoryName": "Whiskey",
84+
"discountPercentage": 7
85+
},
86+
{
87+
"categoryName": "Bitters",
88+
"discountPercentage": 15
89+
},
90+
{
91+
"categoryName": "Brandy",
92+
"discountPercentage": 8
93+
},
94+
{
95+
"categoryName": "Sports Drinks",
96+
"discountPercentage": 22
97+
},
98+
{
99+
"categoryName": "Vodka",
100+
"discountPercentage": 19
101+
}
102+
]
103+
},
104+
{
105+
"eventName": "Steal of a Deal Days",
106+
"promotionalDates": {
107+
"startDate": {
108+
"Year": 2024,
109+
"Month": 9,
110+
"Day": 21
111+
},
112+
"endDate": {
113+
"Year": 2024,
114+
"Month": 9,
115+
"Day": 29
116+
}
117+
},
118+
"discounts": [
119+
{
120+
"categoryName": "Organic Wine",
121+
"discountPercentage": 19
122+
},
123+
{
124+
"categoryName": "White Wine",
125+
"discountPercentage": 20
126+
},
127+
{
128+
"categoryName": "Sparkling Wine",
129+
"discountPercentage": 19
130+
},
131+
{
132+
"categoryName": "Whiskey",
133+
"discountPercentage": 17
134+
},
135+
{
136+
"categoryName": "Vodka",
137+
"discountPercentage": 23
138+
}
139+
]
140+
}
141+
]
142+
}
143+
```
144+
145+
### Example 1 - Return a document that replaces the contents of the original document with a subset of items
146+
147+
First, match a specific document to replace by the _id field and replace the contents of the document with the specified fields.
148+
149+
```mongodb
150+
db.stores.aggregate([{ "$match": { "_id": "bda56164-954d-4f47-a230-ecf64b317b43" } }, { "$replaceWith": { "_id": "$_id", "name": "$name", "sales": "$sales.totalSales" } }])
151+
```
152+
153+
This returns the following result:
154+
```json
155+
{
156+
"_id": "bda56164-954d-4f47-a230-ecf64b317b43",
157+
"name": "Boulder Innovations | Home Security Place - Ankundingburgh",
158+
"sales": 37015
159+
}
160+
```
161+
162+
### Example 2 - Return a document that replaces the contents of the original document after aggregating specified fields
163+
164+
```mongodb
165+
db.stores.aggregate([{ "$match": { "_id": "bda56164-954d-4f47-a230-ecf64b317b43" } }, { "$replaceWith": { "_id": "$_id", "name": "$name", "totalStaff": {"$add": ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"]}}}])
166+
```
167+
168+
This returns the following result:
169+
```json
170+
{
171+
"_id": "bda56164-954d-4f47-a230-ecf64b317b43",
172+
"name": "Boulder Innovations | Home Security Place - Ankundingburgh",
173+
"totalStaff": 29
174+
}
175+
```
176+
## Related content
177+
178+
- [Migrate to vCore based Azure Cosmos DB for MongoDB](https://aka.ms/migrate-to-azure-cosmosdb-for-mongodb-vcore)
179+
- [update with vCore based Azure Cosmos DB for MongoDB](../query-and-write/update.md)
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
title: $sample
3+
titleSuffix: Overview of the $sample operator in Azure Cosmos DB for MongoDB vCore
4+
description: The $sample operator in Azure Cosmos DB for MongoDB vCore returns a randomly selected number of documents
5+
author: abinav2307
6+
ms.author: abramees
7+
ms.service: azure-cosmos-db
8+
ms.subservice: mongodb-vcore
9+
ms.topic: conceptual
10+
ms.date: 02/24/2025
11+
---
12+
13+
# $sample
14+
The `$sample` stage is used in aggregation pipelines to randomly select a specified number of documents from a collection. The `$sample` command is useful during testing, data analysis, and generating random subsets of data for machine learning.
15+
16+
## Syntax
17+
18+
```mongodb
19+
{
20+
"$sample": { "size": <number> }
21+
}
22+
```
23+
24+
### Parameters
25+
26+
| Parameter | Description |
27+
| --- | --- |
28+
| **`size`** | The number of documents to randomly select from the collection|
29+
30+
## Examples
31+
Consider this sample document from the stores collection in the StoreData database.
32+
33+
```json
34+
{
35+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
36+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
37+
"location": {
38+
"lat": -89.2384,
39+
"lon": -46.4012
40+
},
41+
"staff": {
42+
"totalStaff": {
43+
"fullTime": 8,
44+
"partTime": 20
45+
}
46+
},
47+
"sales": {
48+
"totalSales": 75670,
49+
"salesByCategory": [
50+
{
51+
"categoryName": "Wine Accessories",
52+
"totalSales": 34440
53+
},
54+
{
55+
"categoryName": "Bitters",
56+
"totalSales": 39496
57+
},
58+
{
59+
"categoryName": "Rum",
60+
"totalSales": 1734
61+
}
62+
]
63+
},
64+
"promotionEvents": [
65+
{
66+
"eventName": "Unbeatable Bargain Bash",
67+
"promotionalDates": {
68+
"startDate": {
69+
"Year": 2024,
70+
"Month": 6,
71+
"Day": 23
72+
},
73+
"endDate": {
74+
"Year": 2024,
75+
"Month": 7,
76+
"Day": 2
77+
}
78+
},
79+
"discounts": [
80+
{
81+
"categoryName": "Whiskey",
82+
"discountPercentage": 7
83+
},
84+
{
85+
"categoryName": "Bitters",
86+
"discountPercentage": 15
87+
},
88+
{
89+
"categoryName": "Brandy",
90+
"discountPercentage": 8
91+
},
92+
{
93+
"categoryName": "Sports Drinks",
94+
"discountPercentage": 22
95+
},
96+
{
97+
"categoryName": "Vodka",
98+
"discountPercentage": 19
99+
}
100+
]
101+
},
102+
{
103+
"eventName": "Steal of a Deal Days",
104+
"promotionalDates": {
105+
"startDate": {
106+
"Year": 2024,
107+
"Month": 9,
108+
"Day": 21
109+
},
110+
"endDate": {
111+
"Year": 2024,
112+
"Month": 9,
113+
"Day": 29
114+
}
115+
},
116+
"discounts": [
117+
{
118+
"categoryName": "Organic Wine",
119+
"discountPercentage": 19
120+
},
121+
{
122+
"categoryName": "White Wine",
123+
"discountPercentage": 20
124+
},
125+
{
126+
"categoryName": "Sparkling Wine",
127+
"discountPercentage": 19
128+
},
129+
{
130+
"categoryName": "Whiskey",
131+
"discountPercentage": 17
132+
},
133+
{
134+
"categoryName": "Vodka",
135+
"discountPercentage": 23
136+
}
137+
]
138+
}
139+
]
140+
}
141+
```
142+
143+
### Example 1 - Randomly select five documents and project the corresponding document IDs
144+
145+
```mongodb
146+
db.stores.aggregate([{"$sample": {"size": 5}}, {"$project": {"_id": 1}}])
147+
```
148+
149+
This query returns the following results:
150+
```json
151+
[
152+
{ "_id": "f7ae8b40-0c66-4e80-9261-ab31bbabffb4" },
153+
{ "_id": "25350272-6797-4f98-91f8-fe79084755c7" },
154+
{ "_id": "c7fd1d22-1a29-4cb0-9155-1ad71d600c2b" },
155+
{ "_id": "e602b444-9519-42e3-a2e1-b5a3da5f6e64" },
156+
{ "_id": "189c239a-edca-434b-baae-aada3a27a2c5" }
157+
]
158+
```
159+
160+
## Related content
161+
162+
- [Migrate to vCore based Azure Cosmos DB for MongoDB](https://aka.ms/migrate-to-azure-cosmosdb-for-mongodb-vcore)
163+
- [count with vCore based Azure Cosmos DB for MongoDB](count.md)

0 commit comments

Comments
 (0)