Below is the sample Mongo query to fetch the array elements based on the last modified date.
$arrayElemAt: [
'$Invoice',
{
$indexOfArray: [
'$Invoice.LastModifiedDateTime',
{
$max: '$Payments.LastModifiedDateTime'
}
]
}
]
I tried the code in the following way
database.GetCollection<Invoice>(nameof(Invoice)).Aggregate()
.Project(x => new ProjectResult
{
Payments = x.Payments == null ? null : x.Payments.ElementAt(0)
});
Translated Query
{
"$project": {
"Payments": {
"$cond": [
{
"$eq": [
"$Payments",
null
]
},
null,
{
"$arrayElemAt": [
"$Payments",
0
]
}
]
},
"_id": 0
}
}
How to implement the $indexOfArray in the code?