在mongodb中查找嵌套集合

时间:2018-03-29 09:40:02

标签: mongodb mongoose

这里我希望加入多个集合的嵌套连接:

以下是三个集合的模式,即USER,POST和COMMENTS

 const Users = new mongoose.Schema ({
      firstName: {type: String, default: null},
      lastName: {type: String, default: null},
      city:{type: String, default: null},
      country:{type: String, default: null},
      registerTime: {type: Date, default: Date.now},
      email: {type: String, required: true, unique: true},
      password: {type: String, default: null},
      token: {type: String, default: null},
      friends: [{status: String, userid: mongoose.Schema.Types.ObjectId, ftype:String}],
      userId:{type: String, default: null}

    });


const PostSchema = new mongoose.Schema ({

    title: {type: String},
    body: {type: String},
    date: {type: Date,default: Date.now},
    tags:{type: String},
    flag:{type:String},
    _author: {type: mongoose.Schema.Types.ObjectId}

});

const CommentSchema = new mongoose.Schema ({
    comment: {type: String},
    postby: {type: mongoose.Schema.Types.ObjectId},
    date: {type: Date,default: Date.now},
    postid:{type:mongoose.Schema.Types.ObjectId}

});
  

我在下面的Query中创建了一个包含用户详细信息的所有帖子的列表    (发布该帖子的用户加入)。 --Done

     

获取特定于每篇帖子的所有评论 - 完成

到目前为止,我已经成功地为两个文档进行了左连接。我使用以下查询并获得结果。

Posts.aggregate([  
                {"$match": {'_flag': "PUBLISHED"}} ,
                { $sort: { date: -1 } },
                { $lookup: { 
                             from: 'users',
                             localField: '_author',
                             foreignField:'_id', 
                             as:'userDetail'
                            }
                }, // post+ user document

                { $lookup: {
                            from: 'comments', 
                            localField: '_id',
                            foreignField:'postid',
                            as:'commentdata'
                            }
                }  // post+ comments
])

结果是:

{
"status": "Post Listing",
    "posts": [{
        "_id": "5aae21fe58658522243ba9e3",
        "title": "hello zulias;s",
        "tags": "dsadas",
        "flag": "p",
        "_author": "87fe4052dd86c409576fe3e8bc8e6734a024e34d1ee4e841",
        "body": "<p>dsadasda</p>\n",
        "date": "2018-03-18T08:23:26.870Z",
        "__v": 0,
        "userDetail":[{
            "fname": "anurag",
            "lname": "sads",
            "email": "zudsadalia@gmail.com"
        }],

        "commentdata": [{
            "_id": "5aafc8046aa1ec34ac7d8163",
            "postby": "5a993b12303c86133437c681",
            "postid": "5aae21fe58658522243ba9e3",
            "comment": "commment first, ",
            "date": "2018-03-19T14:24:04.231Z",
            "__v": 0
        },
        {
            "_id": "5aafc8076aa1ec34ac7d8164",
            "postby": "5a993b12303c86133437c681",
            "postid": "5aae21fe58658522243ba9e3",
            "comment": "commment first, \nCom ",
            "date": "2018-03-19T14:24:07.760Z",
            "__v": 0
        },
         {
            "_id": "5ab26b07d256913bf8de1cc4",
            "postby": "5a993b2e303c86133437c682",
            "postid": "5aae21fe58658522243ba9e3",
            "comment": "Dasdas",
            "date": "2018-03-21T14:24:07.176Z",
            "__v": 0
        }],

    },

    {
        "_id": "5aad5f207ecf7842bc4ab0f2",
        "title": "sdas",
        "tags": "dsad",
        "flag": "p",
        "_author": "87fe4052dd86c75e576fe3e8bc8e6734a024e34d1ee4e842",
        "body": "<p>sda</p>\n",
        "date": "2018-03-17T18:32:00.194Z",
        "__v": 0,
        "commentdata": [],
        "userDetail":[{
            "fname": "zulia",
            "lname": "Rai",
            "email": "zulia@gmail.com"
        }],
    },

}

问题是,在上面的结果中,每个帖子都有 Commentdata 数组,其中所有评论都是针对特定帖子的。 Commentdata 中的每条评论都与发布该评论的单个用户相关联。所以在这里我希望用户表中的用户名为每个评论     即

"commentdata": [{
                "_id": "5aafc8046aa1ec34ac7d8163",
                "postby": "5a993b12303c86133437c681",  // need name of this user from user table
                "postid": "5aae21fe58658522243ba9e3",
                "comment": "commment first, ",
                "date": "2018-03-19T14:24:04.231Z",
                "__v": 0,
                fname": "zulia", // added after join 
            },

0 个答案:

没有答案