$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: