0% found this document useful (0 votes)
2 views

aggregation _to find array element

The document explains how to use the `$arrayElemAt` operator in a MongoDB aggregation pipeline to retrieve specific elements from an array within a document. It provides a step-by-step example using a collection named `myCollection`, demonstrating how to access elements at specified indices. The example shows how to match a document and project the desired array elements, with the ability to adjust indices for different elements.

Uploaded by

suman_nayak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

aggregation _to find array element

The document explains how to use the `$arrayElemAt` operator in a MongoDB aggregation pipeline to retrieve specific elements from an array within a document. It provides a step-by-step example using a collection named `myCollection`, demonstrating how to access elements at specified indices. The example shows how to match a document and project the desired array elements, with the ability to adjust indices for different elements.

Uploaded by

suman_nayak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

To display array values at a particular position in a MongoDB document, you can use

the `$arrayElemAt` operator within an aggregation pipeline. This operator allows


you to retrieve the element at a specific position within an array.

Here’s a step-by-step example:

1. **Suppose you have a collection named `myCollection` with documents like


this:**

```json
{
"_id": 1,
"name": "John Doe",
"items": [ "item1", "item2", "item3", "item4", "item5" ]
}
```

2. **To display the value at a specific position in the `items` array, use the following
aggregation pipeline:**

```javascript
db.myCollection.aggregate([
{ $match: { _id: 1 } }, // Match the document by _id or other criteria
{ $project: {
name: 1,
specificItem: { $arrayElemAt: ["$items", 2] } // Change 2 to the desired index
}
}
]);
```
In this example, `2` is the index of the element you want to retrieve (MongoDB
arrays are zero-indexed, so 2 refers to the third element, `"item3"`).

3. **The result will be:**

```json
{
"_id": 1,
"name": "John Doe",
"specificItem": "item3"
}
```

You can adjust the index value in the `$arrayElemAt` operator to get the element at
the desired position in the array. If you need to retrieve multiple specific elements,
you can modify the aggregation pipeline accordingly:

```javascript
db.myCollection.aggregate([
{ $match: { _id: 1 } }, // Match the document by _id or other criteria
{ $project: {
name: 1,
itemAtIndex2: { $arrayElemAt: ["$items", 2] }, // Change 2 to the desired index
itemAtIndex4: { $arrayElemAt: ["$items", 4] } // Change 4 to another desired
index
}
}
]);
```

This will return:


```json
{
"_id": 1,
"name": "John Doe",
"itemAtIndex2": "item3",
"itemAtIndex4": "item5"
}
```

This way, you can retrieve and display array values at specific positions in a
MongoDB document.

You might also like