我的结构。
User:
{
name: "One",
favoriteWorkouts: [ids of workouts],
workouts: [ { name: "My workout 1" },...]
}
我想从数据库中获取最喜欢/最热门的锻炼列表。
db.users.aggregate(
{ $unwind : "$favorite" },
{ $group : { _id : "$favorite" , number : { $sum : 1 } } },
{ $sort : { number : -1 } }
)
这返回
{
"hot": [
{
"_id": "521f6c27145c5d515f000006",
"number": 1
},
{
"_id": "521f6c2f145c5d515f000007",
"number": 1
},...
]}
但我想要
{
hot: [
{object of hottest workout 1, object of hottest workout 2,...}
]}
您如何对最热门的数据进行排序并用对象填充结果,而不仅仅是 id?