Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions articles/operators/array-query/$all.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
---
title: $all
titleSuffix: Overview of the $all operator in DocumentDB
description: The $all operator helps finding array documents matching all the elements.
---

# $all

The `$all` operator is used to select documents where the value of a field is an array that contains all the specified elements. This operator is useful when you need to ensure that an array field contains multiple specified elements, regardless of their order in the array.

## Syntax

```javascript
db.collection.find({ <field>: { $all: [ <value1> , <value2> ... ] } })
```

## Parameters

| Parameter | Description |
| --- | --- |
| **`field`** | The field to be queried. |
| **`<value1> , <value2>`** | The values that must all be present in the array field. |

## Examples

Consider this sample document from the stores collection.

```json
{
"_id": "a57511bb-1ea3-4b26-bf0d-8bf928f2bfa8",
"name": "Wide World Importers",
"location": {
"lat": 68.6378,
"lon": -145.2852
},
"staff": {
"totalStaff": {
"fullTime": 1,
"partTime": 5
}
},
"sales": {
"totalSales": 23399,
"salesByCategory": [
{
"categoryName": "Smartphones",
"totalSales": 5231
},
{
"categoryName": "Laptops",
"totalSales": 18168
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2023,
"Month": 5,
"Day": 17
},
"endDate": {
"Year": 2023,
"Month": 5,
"Day": 25
}
},
"discounts": [
{
"categoryName": "Video Games",
"discountPercentage": 20
},
{
"categoryName": "Tablets",
"discountPercentage": 18
}
]
}
],
"tag": [
"#ShopLocal",
"#FashionStore",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
]
}
```

### Example 1: Find documents containing all the specified elements in an array

The example allows querying the `stores` collection to find documents containing both elements `Laptops` and `Smartphones` within `salesByCategory.categoryName` array.

```javascript
db.stores.find(
{ "sales.salesByCategory.categoryName": { $all: ["Laptops", "Smartphones"]} },
{ _id: 1, "sales.salesByCategory.categoryName": 1 }
).limit(2)
```

This query returns the following results.

```json
[
{
"_id": "a57511bb-1ea3-4b26-bf0d-8bf928f2bfa8",
"sales": {
"salesByCategory": [
{
"categoryName": "Smartphones"
},
{
"categoryName": "Laptops"
}
]
}
},
{
"_id": "ca56d696-5208-40c3-aa04-d4e245df44dd",
"sales": {
"salesByCategory": [
{
"categoryName": "Laptops"
},
{
"categoryName": "Smartphones"
}
]
}
}
]
```

## Related content

187 changes: 187 additions & 0 deletions articles/operators/array-query/$elemmatch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
---
title: $elemMatch
titleSuffix: Overview of the $elemMatch operator in DocumentDB
description: The $elemmatch operator returns complete array, qualifying criteria with at least one matching array element.
---

# $elemMatch

The `$elemMatch` operator is used to match documents that contain an array field with at least one element that matches all the specified query criteria. This operator is useful when you need to find array documents with specified element.

## Syntax

```javascript
db.collection.find({ <field>: { $elemMatch: { <query1>, <query2>, ... } } })
```

## Parameters

| Parameter | Description |
| --- | --- |
| **`field`** | The field in the document that contains the array to be queried. |
| **`query`** | The conditions that at least one element in the array must satisfy. |

## Examples

Consider this sample document from the stores collection.

```json
{
"_id": "a57511bb-1ea3-4b26-bf0d-8bf928f2bfa8",
"name": "Wide World Importers",
"location": {
"lat": 68.6378,
"lon": -145.2852
},
"staff": {
"totalStaff": {
"fullTime": 1,
"partTime": 5
}
},
"sales": {
"totalSales": 23399,
"salesByCategory": [
{
"categoryName": "Smartphones",
"totalSales": 5231
},
{
"categoryName": "Laptops",
"totalSales": 18168
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2023,
"Month": 5,
"Day": 17
},
"endDate": {
"Year": 2023,
"Month": 5,
"Day": 25
}
},
"discounts": [
{
"categoryName": "Video Games",
"discountPercentage": 20
},
{
"categoryName": "Tablets",
"discountPercentage": 18
}
]
}
],
"tag": [
"#ShopLocal",
"#FashionStore",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
]
}
```

### Example 1: Find in an array for specific element among the list of elements

The example finds the first two documents in the `stores` collection that have at least one discount with the category name "DJ Lighting" in their `promotionEvents` array. The query only returns the `_id` and `promotionEvents.discounts` fields for those documents.

```javascript
db.stores.find({
"promotionEvents.discounts": {
$elemMatch: {
"categoryName": "DJ Lighting"
}
}
}, {
_id: 1,
"promotionEvents.discounts": 1
}).limit(2)
```

This query returns the following results.

```json
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"promotionEvents": [
{
"discounts": [
{
"categoryName": "DJ Turntables",
"discountPercentage": 18
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 15
}
]
},
{
"discounts": [
{
"categoryName": "DJ Lighting",
"discountPercentage": 14
},
{
"categoryName": "DJ Cases",
"discountPercentage": 20
}
]
}
]
},
{
"_id": "91de5201-8194-44bf-848f-674e8df8bf5e",
"promotionEvents": [
{
"discounts": [
{
"categoryName": "DJ Cases",
"discountPercentage": 6
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 14
}
]
},
{
"discounts": [
{
"categoryName": "DJ Headphones",
"discountPercentage": 19
},
{
"categoryName": "DJ Speakers",
"discountPercentage": 13
}
]
},
{
"discounts": [
{
"categoryName": "DJ Lighting",
"discountPercentage": 12
},
{
"categoryName": "DJ Accessories",
"discountPercentage": 6
}
]
}
]
}
]
```

## Related content

Loading