Skip to content

Commit fbf2e44

Browse files
authored
Merge pull request #2980 from abinav2307/field-update-third-pass-validation
Field update third pass validation
2 parents 35a07a6 + ee7c710 commit fbf2e44

File tree

13 files changed

+674
-999
lines changed

13 files changed

+674
-999
lines changed

articles/cosmos-db/.openpublishing.redirection.cosmos-db.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,26 @@
655655
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/date-expression/$year",
656656
"redirect_document_id": true
657657
},
658+
{
659+
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/operators/field-update/$max.md",
660+
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/accumulators/$max",
661+
"redirect_document_id": true
662+
},
663+
{
664+
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/operators/field-update/$min.md",
665+
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/accumulators/$min",
666+
"redirect_document_id": true
667+
},
668+
{
669+
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/operators/field-update/$set.md",
670+
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/aggregation/$set",
671+
"redirect_document_id": true
672+
},
673+
{
674+
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/operators/field-update/$unset.md",
675+
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/aggregation/$unset",
676+
"redirect_document_id": true
677+
},
658678
{
659679
"source_path_from_root": "/articles/cosmos-db/nosql/troubleshoot-dot-net-sdk-request-timeout.md",
660680
"redirect_url": "/azure/cosmos-db/nosql/troubleshoot-dotnet-sdk-request-timeout",

articles/cosmos-db/mongodb/vcore/operators/accumulators/$max.md

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,41 @@ ms.date: 01/05/2025
1212

1313
# $max
1414

15-
The `$max` operator returns the maximum value of a set of input values. The max operator is particularly useful in identifying the highest value in a dataset, such as maximum sales, discounts, and other numerical comparisons.
15+
The `$max` operator returns the maximum value of a set of input values.
16+
17+
When used as a field update operator, the `$max` operator updates the value of a field to a specified value if the specified value is greater than the current value of the field. If the field does not exist, `$max` creates the field and sets it to the specified value.
1618

1719
## Syntax
20+
1821
```javascript
1922
$max: <expression>
2023
```
2124

25+
When used as a field update operator:
26+
27+
```javascript
28+
{
29+
$max: {
30+
<field1>: <value1>,
31+
<field2>: <value2>,
32+
...
33+
}
34+
}
35+
```
36+
37+
2238
## Parameters
2339
| Parameter | Description |
2440
| --- | --- |
2541
| **`<expression>`** | Any valid expression that resolves to a value. The `$max` operator evaluates this expression to determine the maximum value. |
2642

43+
When used as a field update operator:
44+
45+
| Parameter | Description |
46+
| --- | --- |
47+
| **`field`** | The name of the field to update with the maximum value. |
48+
| **`value`** | The value to compare with the current field value. The field will be updated only if this value is larger. |
49+
2750
## Examples
2851

2952
Consider this sample document from the stores collection.
@@ -289,6 +312,129 @@ The first three results returned by this query are:
289312
]
290313
```
291314

315+
### Example 4: Setting maximum staff capacity (field update operator)
316+
317+
To update the full time staff to 10 only if the current full time staff count is lower, use the $max operator on the field to perform the update. Since the current `fullTime` value is 3, and 10 is greater than 3, the field will be updated to 10.
318+
319+
```javascript
320+
db.stores.updateOne(
321+
{ "_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66" },
322+
{
323+
$max: {
324+
"staff.totalStaff.fullTime": 10
325+
}
326+
}
327+
)
328+
```
329+
330+
### Example 5: Multiple field updates (field update operator)
331+
332+
To update multiple fields with maximum values, use the $max operator with multiple fields and their corresponding max values to set.
333+
334+
```javascript
335+
db.stores.updateOne(
336+
{ "_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66" },
337+
{
338+
$max: {
339+
"staff.totalStaff.partTime": 1,
340+
"sales.totalSales": 50000
341+
}
342+
}
343+
)
344+
```
345+
346+
In this case:
347+
- `partTime` (2) will remain 2 since 1 < 2 (no change)
348+
- `totalSales` (31211) will be updated to 50000 since 50000 > 31211
349+
350+
### Example 6: Creating new fields (field update operator)
351+
352+
If a field doesn't exist, `$max` creates it with the specified value.
353+
354+
```javascript
355+
db.stores.updateOne(
356+
{ "_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66" },
357+
{
358+
$max: {
359+
"staff.maxStaffCapacity": 25,
360+
"sales.peakSalesRecord": 100000
361+
}
362+
}
363+
)
364+
```
365+
366+
### Example 7: Updating array elements (field update operator)
367+
368+
Update maximum values within array elements using positional operators.
369+
370+
```javascript
371+
db.stores.updateOne(
372+
{
373+
"_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
374+
"sales.salesByCategory.categoryName": "Phone Mounts"
375+
},
376+
{
377+
$max: {
378+
"sales.salesByCategory.$.totalSales": 12000
379+
}
380+
}
381+
)
382+
```
383+
384+
### Example 8: Tracking peak performance (field update operator)
385+
386+
Set peak performance metrics that only update when exceeded.
387+
388+
```javascript
389+
db.stores.updateOne(
390+
{ "_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66" },
391+
{
392+
$max: {
393+
"performance.peakDailySales": 5000,
394+
"performance.maxCustomersPerDay": 150,
395+
"performance.highestSalesMonth": 45000
396+
}
397+
}
398+
)
399+
```
400+
401+
After these field update operations, the updated document is:
402+
403+
```json
404+
{
405+
"_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
406+
"name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele",
407+
"staff": {
408+
"totalStaff": {
409+
"fullTime": 10,
410+
"partTime": 2
411+
},
412+
"maxStaffCapacity": 25
413+
},
414+
"sales": {
415+
"totalSales": 50000,
416+
"peakSalesRecord": 100000,
417+
"salesByCategory": [
418+
{
419+
"categoryName": "Phone Mounts",
420+
"totalSales": 12000
421+
},
422+
{
423+
"categoryName": "Dash Cameras",
424+
"totalSales": 22300
425+
}
426+
]
427+
},
428+
"performance": {
429+
"peakDailySales": 5000,
430+
"maxCustomersPerDay": 150,
431+
"highestSalesMonth": 45000
432+
},
433+
"lastPromotionDate": ISODate("2024-12-31T00:00:00.000Z"),
434+
"inventoryDeadline": ISODate("2024-06-30T00:00:00.000Z")
435+
}
436+
```
437+
292438
## Related content
293439

294440
[!INCLUDE[Related content](../includes/related-content.md)]

articles/cosmos-db/mongodb/vcore/operators/accumulators/$min.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ ms.date: 01/05/2025
1414

1515
The `$min` operator is used within aggregation stages like `$group`, `$bucket`, `$bucketAuto`, or `$setWindowFields`. The min operator is particularly useful in summarizing data or finding the smallest value in a dataset.
1616

17+
When used as a field update operator, `$min` operator updates the value of a field to a specified value if the specified value is less than the current value of the field. If the field does not exist, `$min` creates the field and sets it to the specified value.
18+
1719
## Syntax
1820

1921
```javascript
@@ -22,12 +24,31 @@ $min: <expression>
2224

2325
The `<expression>` can be a field path or an aggregation expression that specifies the values to be considered for the minimum calculation.
2426

27+
As a field update operator:
28+
29+
```javascript
30+
{
31+
$min: {
32+
<field1>: <value1>,
33+
<field2>: <value2>,
34+
...
35+
}
36+
}
37+
```
38+
2539
## Parameters
2640

2741
| Parameter | Description |
2842
| --- | --- |
2943
| **`<expression>`** | Specifies the field or computed value to determine the minimum value. |
3044

45+
As a field update operator:
46+
47+
| Parameter | Description |
48+
| --- | --- |
49+
| **`field`** | The name of the field to update with the minimum value. |
50+
| **`value`** | The value to compare with the current field value. The field will be updated only if this value is smaller. |
51+
3152
## Examples
3253

3354
Consider this sample document from the stores collection.
@@ -350,6 +371,123 @@ This query returns the following results.
350371
]
351372
```
352373

374+
### Example 4: Setting minimum staff requirements (field update operator)
375+
376+
To set a minimum staff requirement, update the full time stagg count only if the current value of the field is higher. Since the current `fullTime` value is 14, and 10 is less than 14, the field will be updated to 10.
377+
378+
```javascript
379+
db.stores.updateOne(
380+
{ "_id": "26afb024-53c7-4e94-988c-5eede72277d5" },
381+
{
382+
$min: {
383+
"staff.totalStaff.fullTime": 10
384+
}
385+
}
386+
)
387+
```
388+
389+
### Example 5: Multiple field updates (field update operator)
390+
391+
To update multiple fields with minimum values simultaneously, use the $min operator with multiple fields and corresponding min values.
392+
393+
```javascript
394+
db.stores.updateOne(
395+
{ "_id": "26afb024-53c7-4e94-988c-5eede72277d5" },
396+
{
397+
$min: {
398+
"staff.totalStaff.partTime": 12,
399+
"sales.totalSales": 50000
400+
}
401+
}
402+
)
403+
```
404+
405+
In this case:
406+
- `partTime` (8) will be updated to 8 since 12 > 8 (no change)
407+
- `totalSales` (83865) will be updated to 50000 since 50000 < 83865
408+
409+
### Example 6: Creating new fields (field update operator)
410+
411+
If a field doesn't exist, `$min` creates it with the specified value.
412+
413+
```javascript
414+
db.stores.updateOne(
415+
{ "_id": "26afb024-53c7-4e94-988c-5eede72277d5" },
416+
{
417+
$min: {
418+
"staff.minStaffRequired": 15,
419+
"sales.minimumSalesTarget": 30000
420+
}
421+
}
422+
)
423+
```
424+
425+
### Example 7: Working with Dates (field update operator)
426+
427+
Set minimum dates for tracking earliest events.
428+
429+
```javascript
430+
db.stores.updateOne(
431+
{ "_id": "26afb024-53c7-4e94-988c-5eede72277d5" },
432+
{
433+
$min: {
434+
"lastInventoryCheck": new Date("2024-01-15"),
435+
"firstSaleDate": new Date("2023-06-01")
436+
}
437+
}
438+
)
439+
```
440+
441+
### Example 8: Updating array elements (field update operator)
442+
443+
Update minimum values within array elements using positional operators.
444+
445+
```javascript
446+
db.stores.updateOne(
447+
{
448+
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
449+
"sales.salesByCategory.categoryName": "Lavalier Microphones"
450+
},
451+
{
452+
$min: {
453+
"sales.salesByCategory.$.totalSales": 40000
454+
}
455+
}
456+
)
457+
```
458+
459+
After these field update operations, the updated document is:
460+
461+
```json
462+
{
463+
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
464+
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
465+
"staff": {
466+
"totalStaff": {
467+
"fullTime": 10,
468+
"partTime": 8
469+
},
470+
"minStaffRequired": 15
471+
},
472+
"sales": {
473+
"totalSales": 50000,
474+
"minimumSalesTarget": 30000,
475+
"salesByCategory": [
476+
{
477+
"categoryName": "Lavalier Microphones",
478+
"totalSales": 40000
479+
},
480+
{
481+
"categoryName": "Wireless Microphones",
482+
"totalSales": 39691
483+
}
484+
]
485+
},
486+
"lastInventoryCheck": ISODate("2024-01-15T00:00:00.000Z"),
487+
"firstSaleDate": ISODate("2023-06-01T00:00:00.000Z")
488+
}
489+
```
490+
353491
## Related content
354492

355493
[!INCLUDE[Related content](../includes/related-content.md)]

0 commit comments

Comments
 (0)