mgo是否具有根据查询结果对每个文档的特定“切片”字段进行排序的功能?

I'm not talking about how to sort result. That I know!
It's one of the field in result that I want sorted in a specific order.
So let say we have a result := []A{} returned by a mgo query:

type A struct {
    I  string
    II []B
}
type B struct {
    X  string
    XX int
}

In that result, I want each result[i].II sorted by B.XX

What I'm asking is can I get this kind of sort done by mgo or I'll have to loop on result and sort the A.II Slice my self ?

There is a way to do it with the aggregation engine.

  1. $match the documents you want
  2. $project the fields you want
  3. $unwind the slice
  4. $sort the unwound slice using the id and the unwound value as your sort keys
  5. $group your sorted slice elements together