【发布时间】:2015-03-20 08:21:42
【问题描述】:
我在 Mongo 中有以下文档架构:
{
"_id": "an id",
"title": "my title",
"muted": "true",
"participants": [{ "userid":12345, "more": "data" }, ... ],
"messages": [{ "message", "details " } ... { } ]
}
我正在尝试以这种形式获取一组对象:
[
{
"_id": "an id",
"meta"
{
"title": "my title",
"muted": "true",
"participants": [12345, /* user ids only */ ],
},
"messages": [{ "message", "details " } ... { } ]
}
]
我已经让聚合生成消息和_id:
[
{ $match:
{
'participants.user_id': userId
}
},
{ $unwind: "$messages" },
{ $match: { 'messages.sent_at': { '$gte': new Date(date) }}},
{ $group: { _id: "$_id", messages: { $addToSet: "$messages" }}}
]
获取元数据需要什么魔法?
【问题讨论】:
-
在我的解决方案中,我将 'participants.user_id' 更改为 'participants.userid' 并删除了 $match,因为示例文档没有 sent_at 归档
-
我用关于参与者的两种可能性更新了答案
标签: mongodb mongodb-query aggregation-framework