0

I have a $lookup stage in an mongodb aggregation framework request that adds an array of related documents to each requested document.

  $lookup: {
    from: 'cats',
    localField: '_id',
    foreignField: 'cat',
    as: 'cats',
  }

So each document in the result has a field with an array like this:

 cats: [{ name: "cat7"),{ name: "cat2"), ...]

How can I get each array sorted e.g. by name? Is there an aggregation step for this?

 cats: [{ name: "cat2" }),{ name: "cat7" ), ...]
PeteMeier
  • 521
  • 1
  • 6
  • 18
  • Still no other way at present to sort an array in the aggregation framework. With MongoDB 3.6 you can actually "sort" the results of lookup before they are returned. But not yet. – Neil Lunn Sep 11 '17 at 11:08
  • @Neil: thank you! (I wasn't aware of the other question...) – PeteMeier Sep 11 '17 at 14:43

0 Answers0