Skip to content

Commit

Permalink
Merge pull request #228 from MicrosoftDocs/main
Browse files Browse the repository at this point in the history
Publish to Live Thursday 4AM PST, 9/12
  • Loading branch information
PMEds28 authored Sep 12, 2024
2 parents 6eaa491 + 71f9c80 commit a43f81c
Show file tree
Hide file tree
Showing 34 changed files with 841 additions and 155 deletions.
25 changes: 25 additions & 0 deletions articles/cosmos-db/.openpublishing.redirection.cosmos-db.json
Original file line number Diff line number Diff line change
Expand Up @@ -5834,6 +5834,31 @@
"source_path_from_root": "/articles/cosmos-db/how-to-move-regions.md",
"redirect_url": "/azure/operational-excellence/relocation-cosmos-db",
"redirect_document_id": false
},
{
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/commands/aggregation/$count.md",
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/aggregation/$count",
"redirect_document_id": true
},
{
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/commands/array-query/$size.md",
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/array-query/$size",
"redirect_document_id": true
},
{
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/commands/array-update/$push.md",
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/array-update/$push",
"redirect_document_id": true
},
{
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/commands/index.yml",
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/index",
"redirect_document_id": true
},
{
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/commands/projection/$slice.md",
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/projection/$slice",
"redirect_document_id": true
}
]
}
4 changes: 2 additions & 2 deletions articles/cosmos-db/mongodb/vcore/TOC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@
href: how-to-private-link.md
- name: Reference
items:
- name: Commands
href: commands/toc.yml
- name: Operators
href: operators/toc.yml
- name: Release notes
href: release-notes.md
- name: Troubleshoot common issues
Expand Down
10 changes: 0 additions & 10 deletions articles/cosmos-db/mongodb/vcore/commands/TOC.yml

This file was deleted.

34 changes: 0 additions & 34 deletions articles/cosmos-db/mongodb/vcore/commands/array-query/$size.md

This file was deleted.

39 changes: 0 additions & 39 deletions articles/cosmos-db/mongodb/vcore/commands/index.yml

This file was deleted.

53 changes: 0 additions & 53 deletions articles/cosmos-db/mongodb/vcore/commands/projection/$slice.md

This file was deleted.

22 changes: 22 additions & 0 deletions articles/cosmos-db/mongodb/vcore/operators/TOC.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
- name: Index
href: index.yml
- name: Aggregation
href: aggregation/toc.yml
- name: Array expression
href: array-expression/toc.yml
- name: Array query
href: array-query/toc.yml
- name: Array update
href: array-update/toc.yml
- name: Bitwise query
href: bitwise-query/toc.yml
- name: Comparison query
href: comparison-query/toc.yml
- name: Evaluation query
href: evaluation-query/toc.yml
- name: Geospatial
href: geospatial/toc.yml
- name: Logical query
href: logical-query/toc.yml
- name: Projection
href: projection/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ ms.author: gahllevy
ms.service: azure-cosmos-db
ms.subservice: mongodb-vcore
ms.topic: reference
ms.date: 09/09/2024
ms.date: 09/11/2024
---

# `$count` (as aggregation pipeline stage)
# `$count` (aggregation pipeline stage)

[!INCLUDE[MongoDB (vCore)](~/reusable-content/ce-skilling/azure/includes/cosmos-db/includes/appliesto-mongodb-vcore.md)]

Expand All @@ -28,7 +28,7 @@ The `$count` stage in the aggregation pipeline is used to count the number of do

| | Description |
| --- | --- |
| **`<field>`** | The name of the field in the output document where the count is stored. |
| **`field`** | The name of the field in the output document where the count is stored. |

## Related content

Expand Down
127 changes: 127 additions & 0 deletions articles/cosmos-db/mongodb/vcore/operators/array-expression/$slice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
title: $slice (array expression) usage on Azure Cosmos DB for MongoDB vCore
titleSuffix: Azure Cosmos DB for MongoDB vCore
description: The $slice operator returns a subset of an array from any element onwards in the array.
author: avijitgupta
ms.author: avijitgupta
ms.service: azure-cosmos-db
ms.subservice: mongodb-vcore
ms.topic: reference
ms.date: 09/11/2024
---

# $slice (Array Expression)

[!INCLUDE[MongoDB (vCore)](~/reusable-content/ce-skilling/azure/includes/cosmos-db/includes/appliesto-mongodb-vcore.md)]

The `$slice` operator is used to return a subset of an array. It can be used to limit the number of elements in an array to a specified number or to return elements from a specified position in the array. This is particularly useful when dealing with large arrays where only a portion of the data is needed for processing or display.

## Syntax

The syntax for the `$slice` operator is as following.

- Returns elements from either the start or end of the array

```javascript
{ $slice: [ <array>, <n> ] }
```

- Returns elements from the specified position in the array

```javascript
{ $slice: [ <array>, <position>, <n> ] }
```

## Parameters

| | Description |
| --- | --- |
| **`array`** | Any valid array expression. |
| **`position`** | Any valid integer expression. |
| **`n`** | Any valid integer expression. |

## Example

Let's understand the usage with sample json from `stores` dataset.

```json
{
"_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
"name": "Lakeshore Retail",
"sales": {
"salesByCategory": [
{
"categoryName": "Towel Racks",
"totalSales": 13237
},
{
"categoryName": "Washcloths",
"totalSales": 44315
},
{
"categoryName": "Face Towels",
"totalSales": 42095
},
{
"categoryName": "Toothbrush Holders",
"totalSales": 47912
},
{
"categoryName": "Hybrid Mattresses",
"totalSales": 48660
},
{
"categoryName": "Napkins",
"totalSales": 31439
},
{
"categoryName": "Pillow Cases",
"totalSales": 38833
}
]},
"tag": [
'#ShopLocal',
'#FashionStore',
'#SeasonalSale',
'#FreeShipping',
'#MembershipDeals'
]
}
```

### Example 1: Return the set of elements from the array

The example queries for the first three elements of the `sales.salesByCategory` array for `_id: 988d2dd1-2faa-4072-b420-b91b95cbfd60` within `stores` collection.

```javascript
db.stores.aggregate([
{ $match: { "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60"} }
, { $project: { "salesByCategory": { $slice: [ "$sales.salesByCategory", 3 ] } } }
])
```

The query response reverts with first three array elements for the sample json.

```json
{
"_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
"salesByCategory": [
{
"categoryName": "Towel Racks",
"totalSales": 13237
},
{
"categoryName": "Washcloths",
"totalSales": 44315
},
{
"categoryName": "Face Towels",
"totalSales": 42095
}
]
}
```

## Related content

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

0 comments on commit a43f81c

Please sign in to comment.