我的用户模型有以下数据结构:
{
_id: object,
name: string,
quotes: [ { quote: string, createdAt: date } ],
friends: [ { _id: object } ] (this refers to the _id from the user model)
}
我想做的是回复:
{
quote: string,
createdAt: date,
_id: object (friend's)
}
{
quote: string,
createdAt: date,
_id: object (friend's)
}
...
按createdAt = recent排序到最早
答案 0 :(得分:1)
我从你的问题中理解你想要根据最近的时间解开两个数组元素。
db.user.aggregate([
{
$unwind: "$quotes"
},
{
$unwind: "$friends"
},
{
$sort: {
"quotes.createdAt": -1
}
}
])