Reputation: 683
I'm trying to sort an array (descending) that gets computed through a $lookup
by a field called createdAt
which contains a Date
object:
[
{
"$lookup": {
"from": "activities",
"localField": "activities",
"foreignField": "_id",
"as": "activities"
}
},
{
"$sort": {
"activities.createdAt": -1
}
},
{
"$project": {
"_id": false,
"updatedAt": false,
"activities._id": false,
"activities.game": false,
"activities.updatedAt": false,
"activities.__v": false,
"__v": false
}
}
]
The activities
field is being "populated" correctly but it's not sorting the results.
Upvotes: 0
Views: 529
Reputation: 8695
Try to replace your lookup with a lookup with pipeline like the bellow. If you want the array to be sorted, you can do it inside that pipeline.
If this isn't what you need if you can add some sample data and expected output.
aggregate(
[{"$lookup":
{"from": "activities",
"localField": "activities",
"foreignField": "_id",
"pipeline": [{"$sort": {"createdAt": -1}}],
"as": "activities"}}])
Upvotes: 1