【发布时间】:2016-10-13 07:49:41
【问题描述】:
我总共有 3 个收藏: 1. 用户 2.帖子 3. 动作
我的收藏如下所示:
用户:
{
"_id" : ObjectId("57ee65fef5a0c032877725db"),
"fbId" : "EAAXZA4sZCZBKmgBAKe0JpJPrp7utWME6xbHT9yFD",
"name" : "Aftab",
"email" : "xxxe@hotmail.com",
"gender" : "male",
"__v" : NumberInt(0),
"updatedAt" : ISODate("2016-10-10T05:11:35.344+0000"),
"score" : NumberInt(90)
}
行动:
{
"_id" : ObjectId("57f7a0ba3a603627658afdd3"),
"updatedAt" : ISODate("2016-10-07T13:18:50.815+0000"),
"createdAt" : ISODate("2016-10-07T13:18:50.815+0000"),
"userId" : ObjectId("57ee65fef5a0c032877725db"),
"postId" : ObjectId("57f4b5e98899081203883a1b"),
"type" : "like"
}
{
"_id" : ObjectId("57f7a0ba3a603627658afdd4"),
"updatedAt" : ISODate("2016-10-07T13:18:50.815+0000"),
"createdAt" : ISODate("2016-10-07T13:18:50.815+0000"),
"userId" : ObjectId("57ee65fef5a0c032877725db"),
"postId" : ObjectId("57f4b5d58899081203883a1a"),
"type" : "dismiss"
}
帖子:
{
"_id" : ObjectId("57f24593e272b5199e9351b9"),
"imgFileLocation" : "http://xxxx/buybye-platform/uploads/image-1475495315229",
"description" : "cool cool",
"title" : "Bad Image ",
"userId" : ObjectId("57f21e3d0b787d0f7ad76dd0"),
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("57f4b5d58899081203883a1a"),
"imgFileLocation" : "http://xxx/buybye-platform/uploads/image-1475655125125",
"description" : "cool & cool",
"title" : "Good Image",
"userId" : ObjectId("57f21e3d0b787d0f7ad76dd0"),
"__v" : NumberInt(0)
}
用户可以创建帖子,其他用户可以对这些帖子执行操作
posts 集合有 userId 的引用 并且操作集合具有 userId(谁执行了该操作)、postId(在哪个帖子上)和 action-type(喜欢/不喜欢/解雇)的 ref
我需要查询以获取对特定用户帖子执行的所有操作
我能够获取针对某个用户的所有帖子,这非常简单并且是一个数组。现在我需要在这个帖子数组的每个帖子上执行所有操作。
【问题讨论】:
-
您是否愿意使用集合中的一些示例文档和所需聚合的预期输出来更新您的问题?这有助于澄清问题并鼓励提供高质量的答案。
-
你运行的是哪个 MongoDB 版本?
-
mongodb v3.2 @Styvane
标签: node.js mongodb mongoose mongodb-query aggregation-framework