Skip to content
Closed
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
179 changes: 179 additions & 0 deletions reference/operators/array-expression/$arrayelemat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
---
title: $arrayElemAt
description: The $arrayElemAt returns the element at the specified array index.
type: operators
category: array-expression
---

# $arrayElemAt

The `$arrayElemAt` operator is used to return the element at the specified array index. This operator is helpful when you need to extract a specific element from an array within your documents.

## Syntax

```javascript
{
$arrayElemAt: ["<array>", <idx>]
}
```

## Parameters

| Parameter | Description |
| --- | --- |
| **`<array>`**| The array reference from which the element is retrieved.|
| **`<idx>`**| The index of the element to return. The index is zero-based. A negative index counts from the end of the array.|

## Examples

Consider this sample document from the stores collection.

```json
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"location": {
"lat": 60.1441,
"lon": -141.5012
},
"staff": {
"totalStaff": {
"fullTime": 2,
"partTime": 0
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Cables",
"totalSales": 1000
},
{
"categoryName": "DJ Headphones",
"totalSales": 35921
}
],
"fullSales": 3700
},
"promotionEvents": [
{
"eventName": "Cyber Monday Event",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 8,
"Day": 1
},
"endDate": {
"Year": 2024,
"Month": 8,
"Day": 7
}
},
"discounts": [
{
"categoryName": "DJ Speakers",
"discountPercentage": 25
}
]
},
{
"eventName": "Black Friday Event",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 8,
"Day": 1
},
"endDate": {
"Year": 2024,
"Month": 8,
"Day": 7
}
},
"discounts": [
{
"categoryName": "DJ Speakers",
"discountPercentage": 25
}
]
},
{
"eventName": "Mega Discount Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 5,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 5,
"Day": 18
}
},
"discounts": [
{
"categoryName": "DJ Lights",
"discountPercentage": 20
}
]
}
],
"tag": [
"#ShopLocal",
"#NewArrival",
"#FashionStore",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
]
}

```

### Example 1: Return the first element from an array field

This query retrieves the first event details from `promotionEvents` array for the searched store.

```javascript
db.stores.aggregate([
{ $match: { name: "Lakeshore Retail | DJ Equipment Stop - Port Cecile" } },
{
$project: {
firstPromotionEvent: { $arrayElemAt: ["$promotionEvents", 0] }
}
}
])
```

This query returns the following result.

```json
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"firstPromotionEvent": {
"eventName": "Cyber Monday Event",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 8,
"Day": 1
},
"endDate": {
"Year": 2024,
"Month": 8,
"Day": 7
}
},
"discounts": [
{
"categoryName": "DJ Speakers",
"discountPercentage": 25
}
]
}
}
]
```
152 changes: 152 additions & 0 deletions reference/operators/array-expression/$arraytoobject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
---
title: $arrayToObject
description: The $arrayToObject allows converting an array into a single document.
type: operators
category: array-expression
---

# $arrayToObject

The `$arrayToObject` operator is used to convert an array into a single document. This operator is useful when you need to transform arrays of key-value pairs into a more structured document format.

## Syntax

```javascript
{
$arrayToObject: "<array>"
}
```

## Parameters

| Parameter | Description |
| --- | --- |
| **`<array>`**| The array to be converted into a document. Each element in the array must be either: a) A two-element array where the first element is the field name and the second element is the field value. b) A document with exactly two fields, "k" and "v", where "k" is the field name and "v" is the field value.|

## Examples

Consider this sample document from the stores collection.

```json
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"location": {
"lat": 60.1441,
"lon": -141.5012
},
"staff": {
"totalStaff": {
"fullTime": 2,
"partTime": 0
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Headphones",
"totalSales": 35921
},
{
"categoryName": "DJ Cables",
"totalSales": 1000
}
],
"fullSales": 3700
},
"promotionEvents": [
{
"eventName": "Bargain Blitz Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 2,
"Day": 18
}
},
"discounts": [
{
"categoryName": "DJ Turntables",
"discountPercentage": 18
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 15
}
]
},
{
"eventName": "Discount Delight Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 5,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 5,
"Day": 18
}
}
}
],
"tag": [
"#ShopLocal",
"#FashionStore",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
]
}
```

### Example 1: Convert the array to a key: value document

This query converts the `salesByCategory` array into an object where each `categoryName` is a key and `totalSales` is the corresponding value. This transformation simplifies access to sales data by category directly from an object structure.

```javascript
db.stores.aggregate([{
$match: {
_id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}
},
{
$project: {
"sales.salesByCategory": {
$arrayToObject: {
$map: {
input: "$sales.salesByCategory",
as: "item",
in: {
k: "$$item.categoryName",
v: "$$item.totalSales"
}
}
}
}
}
}
])
```

The query returns the following result.

```json
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"sales": {
"salesByCategory": {
"DJ Headphones": 35921,
"DJ Cables": 1000
}
}
}
]
```
Loading