$indexOfArray - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$indexOfArray

The $indexOfArray operator in Amazon DocumentDB is used to find the index of the first occurrence of a specified element in an array. This operator returns the zero-based index position of the first element in the array that matches the specified value. If the value is not found, it returns -1.

Parameters

  • array: The array to search.

  • value: The value to search for in the array.

  • start: (optional) The position in the array to start the search from. The default value is 0.

Example (MongoDB Shell)

The following example demonstrates how to use the $indexOfArray operator to find the index of the first occurrence of the element "mango" in the "fruits" array for each document.

Create sample documents

db.collection.insertMany([ { _id: 1, fruits: ["apple", "banana", "cherry", "durian"] }, { _id: 2, fruits: ["mango", "orange", "pineapple"] }, { _id: 3, fruits: ["kiwi", "lemon", "mango"] } ]);

Query example

db.collection.aggregate([ { $project: { _id: 1, fruitIndex: { $indexOfArray: ["$fruits", "mango"] } } } ]);

Output

{ "_id" : 1, "fruitIndex" : 1 } { "_id" : 2, "fruitIndex" : 0 } { "_id" : 3, "fruitIndex" : 2 }

Code examples

To view a code example for using the $indexOfArray 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('collection'); const result = await collection.aggregate([ { $project: { _id: 1, fruitIndex: { $indexOfArray: ["$fruits", "mango"] } } } ]).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['test'] collection = db['collection'] result = list(collection.aggregate([ { '$project': { '_id': 1, 'fruitIndex': { '$indexOfArray': ["$fruits", "mango"] } } } ])) print(result) client.close() example()