## How to use $arrayElemAt operator in Spring Data MongoDB

mongodb aggregate match in array of objects

mongodb return only matching array elements

mongodb find array element by index

mongodb project first element of array

mongodb aggregation framework operators

mongodb addfields array

mongodb aggregate array

$arrayElemAt new in MongoDB version 3.2.

db.users.aggregate([ { $project: { name: 1, first: { $arrayElemAt: [ "$favorites", 0 ] }, last: { $arrayElemAt: [ "$favorites", -1 ] } } }

])

DATAMONGO-1536 is about to add `$arrayElemAt`

and other missing aggregation operators to Spring Data MongoDB for the Ingalls release. Meanwhile you can provide your own `AggregationExpression`

to create whatever operation is needed.

For the above something like the following does the trick:

project("name") // .and(context -> new BasicDBObject("$arrayElemAt", asList("$favorites", 0))).as("first") .and(context -> new BasicDBObject("$arrayElemAt", asList("$favorites", -1))).as("last");

**ArrayOperators.ArrayElemAt (Spring Data MongoDB 3.0.1 ,** protected static class, org.springframework.data.mongodb.core.aggregation. Use the element at the index number traken from the given field. protected <T> T The <idx> expression can be any valid expression as long as it resolves to an integer. If positive, $arrayElemAt returns the element at the idx position, counting from the start of the array. If negative, $arrayElemAt returns the element at the idx position, counting from the end of the array. If the idx exceeds the array bounds, $arrayElemAt does not return any result.

you can now use `$arrayElemAt`

with ArrayOperators.ArrayElemAt class.

The above MongoDB projection would be traduced like the following for Spring Data Mongo:

project("name") .and(ArrayOperators.ArrayElemAt.arrayOf("favorites").elementAt(0)).as("first") .and(ArrayOperators.ArrayElemAt.arrayOf("favorites").elementAt(-1)).as("last")

**$arrayElemAt (aggregation),** New in version 3.2. Returns the element at the specified array index. $arrayElemAt has the following syntax: copy. copied. new instance of ArrayOperators.ArrayElemAt. Since: 2.2; elementAt public ArrayOperators.ArrayElemAt elementAt(int index) elementAt public ArrayOperators.ArrayElemAt elementAt(AggregationExpression expression) elementAt public ArrayOperators.ArrayElemAt elementAt(String arrayFieldReference) toDocument

Even more concise, you can even use `$arrayElementAt`

as follows:

project("name") .and("favorites").arrayElementAt(0).as("first") .and("favorites").arrayElementAt(-1).as("last")

**How to use $arrayElemAt operator in Spring Data MongoDB,** DATAMONGO-1536 is about to add $arrayElemAt and other missing aggregation operators to Spring Data MongoDB for the Ingalls release. Meanwhile you can Stack Overflow Public questions and answers; I have been forced to used Mongo 3.0 so I can't use this operator, which is ideal for what I want to do

**Truncation of Dates in Aggregation,** As of 3.6 there's a slightly simpler way to achieve the same thing using the To convert to "YYYY-MM-DD" we can use $split and $arrayElemAt expressions: So if the values we have are [ 100, 96, 96, 25, 25, 1 ] then ranks would be [ 1, 2, 2, 4, This is possible thanks to the new operator "$zip" which follows the Python $filter AggregationExpression allows to select a subset of the array to return based on the specified condition.

**spring-projects/spring-data-mongodb,** The aggregation pipeline operators construct expressions for use in the Some arithmetic expression also supports data arithmetic. $arrayElemAt. It returns The following query matches only those documents where the results array contains at least one element that is both greater than or equal to 80 and is less than 85.

**ArrayOperators.ArrayElemAt (Spring Data MongoDB 2.1.0 ,** DATAMONGO-1536 is about to add $arrayElemAt and other missing aggregation operators to Spring Data MongoDB for the Ingalls release. Meanwhile you can System.InvalidOperationException: Unable to determine the serialization information for x => x.Bars.First(). at MongoDB.Driver.ExpressionFieldDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)