Mongodb在同一聚合管道中多个$ lookup和$ unwind

时间:2019-05-29 08:41:16

标签: javascript node.js mongodb aggregation-framework

嘿,我有两个mongo DB集合,一个用于存储帖子,另一个包含用户详细信息,

  

这是我的用户的集合>>

{
  _id: objectId(1111111111),
  name: "userOne",
  email: userone@user.com
  picture: "link to profile picture"
},

{
  _id: objectId(2222222222),
  name: "userTwo",
  email: usertwo@user.com
  picture: "link to profile picture"
},

{
  _id: objectId(3333333333),
  name: "userThree",
  email: userthree@user.com
  picture: "link to profile picture"
}

  

这是帖子集>>

{
  _id: objectId(44444444444),
  title : "Title of the Post",
  content: "Some Content",
  author: objectId(3333333333)
  likes: [
     0: {likeBy:objectId(1111111111),
         Date: "Date of Like"},

     1: {likeBy:objectId(2222222222),
         Date: "Date of Like"}
   ],
  comments: [
     0 : {user: objectId(1111111111),
          comment: "Nice",
          Date: "Date of comment" },

     1 : {user: objectId(2222222222),
          comment: "Good",
          Date: "Date of comment" }
   ]
}

所以我的目标是使用MongoDB聚合框架获取作者详细信息,喜欢的用户详细信息,评论的用户详细信息,该如何做, 我已经尝试过进行聚合操作,但是失败了,有人可以引导我以正确的方式进行操作

  

这是我想要的输出


{
  _id: objectId(44444444444),
  title : "Title of the Post",
  content: "Some Content",
  author: {objectId(3333333333),
           name: "author's name from user collection",
           picture: "author's picture link form user collection" }
  likes: [
     0: {likeBy:objectId(1111111111),
         name: "user's name from user collection",
         picture: "user's picture link form user collection",
         Date: "Date of Like"},

     1: {likeBy:objectId(2222222222),
         name: "user's name from user collection",
         picture: "user's picture link form user collection",
         Date: "Date of Like"}
   ],
  comments: [
     0 : {user: objectId(1111111111),
          comment: "Nice",
          name: "user's name from user collection",
          picture: "user's picture link form user collection",
          Date: "Date of comment" },

     1 : {user: objectId(2222222222),
          comment: "Good",
          name: "user's name from user collection",
          picture: "user's picture link form user collection",
          Date: "Date of comment" }
   ]
}

0 个答案:

没有答案