$arrayElemAt - Amazon DocumentDB

$arrayElemAt

The $arrayElemAt operator in Amazon DocumentDB allows you to retrieve an element from an array by its index position. This is particularly useful when you need to access a specific element within an array field in your documents.

Parameters

  • array: The input array from which an element is to be retrieved.

  • index: The zero-based index position of the element to retrieve. This value must be a non-negative integer.

Example (MongoDB Shell)

In this example, we'll demonstrate how to use the $arrayElemAt operator to retrieve specific elements from the flight_miles array in the miles collection.

Create sample documents

db.miles.insertMany([ { "_id" : 1, "member_since" : ISODate("1987-01-01T00:00:00Z"), "credit_card" : false, "flight_miles" : [ 1205, 2560, 880 ]}, { "_id" : 2, "member_since" : ISODate("1982-01-01T00:00:00Z"), "credit_card" : true, "flight_miles" : [ 1205, 2560, 890, 2780 ]}, { "_id" : 3, "member_since" : ISODate("1999-01-01T00:00:00Z"), "credit_card" : true, "flight_miles" : [ 1205, 880 ]} ]);

Query example

db.miles.aggregate([ { $project: { "_id": 1, "first_mile": { $arrayElemAt: [ "$flight_miles", 0 ] }, "last_mile": { $arrayElemAt: [ "$flight_miles", -1 ] } }} ]);

Output

{ "_id" : 1, "first_mile" : 1205, "last_mile" : 880 } { "_id" : 2, "first_mile" : 1205, "last_mile" : 2780 } { "_id" : 3, "first_mile" : 1205, "last_mile" : 880 }

In this example, we use the $arrayElemAt operator to retrieve the first and last elements of the flight_miles array for each document.

Code examples

To view a code example for using the $arrayElemAt command, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('miles'); const result = await collection.aggregate([ { $project: { "_id": 1, "first_mile": { $arrayElemAt: [ "$flight_miles", 0 ] }, "last_mile": { $arrayElemAt: [ "$flight_miles", -1 ] } }} ]).toArray(); console.log(result); client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.mydatabase collection = db.miles result = list(collection.aggregate([ { "$project": { "_id": 1, "first_mile": { "$arrayElemAt": [ "$flight_miles", 0 ] }, "last_mile": { "$arrayElemAt": [ "$flight_miles", -1 ] } }} ])) print(result) client.close() example()