mongoose - 从3级深度数组

时间:2018-02-24 19:56:42

标签: arrays mongodb mongoose nested

您好我有这样的架构

    account: {
    blogPosts: [{
        title: String,
        text: String,
        date: String,
        author: String,
        editDate: String,
        comments: [{
            username: String,
            text: String,
            date: String,
            likes: Array
        }]
    }]
},

我想要做的是当用户喜欢blogPost上的评论时我想将他们的用户名推送到likes数组。首先,我必须找到帖子来访问与该帖子相关的评论。然后我必须找到他们喜欢的评论并将名称推到喜欢的数组。

所以这就是我到目前为止所提出的

   app.post('/server-activity/like-blog-comment', (req, res) => {
    const username = req.user.account.username;
    const blogAuthor = req.body.blogAuthor;
    const blogId = req.body.blogId;
    const commentId = req.body.commentId;

    userCollection.findOne({
        'account.username': blogAuthor
    }, function(err, obj) {
        if (err) return err;

        for (var i = 0; i < obj.account.blogPosts.length; i++) {
            if (blogId == obj.account.blogPosts[i]._id) {

                for (var x = 0; x < obj.account.blogPosts[i].comments.length; x++) {
                    if (commentId == obj.account.blogPosts[i].comments[x]._id) {

                        if(obj.account.blogPosts[i].comments[x].likes.indexOf(username) === -1){
                        obj.account.blogPosts[i].comments[x].likes.unshift(username);
                        obj.save(err => {
                            if (err) return err
                            res.send('success');
                        });
                        }
                    }
                }

            }
        }

    });
});

按预期工作,但它非常混乱我想知道是否有办法只用猫鼬做这个?

同样对于不喜欢评论它的工作方式几乎相同,只是我将用户从喜欢的数组中拼接如下

    app.post('/server-activity/unlike-blog-comment', (req, res) => {
    const username = req.user.account.username;
    const blogAuthor = req.body.blogAuthor;
    const blogId = req.body.blogId;
    const commentId = req.body.commentId;

    userCollection.findOne({
        'account.username': blogAuthor
    }, function(err, obj) {
        if (err) return err;

        for (var i = 0; i < obj.account.blogPosts.length; i++) {
            if (blogId == obj.account.blogPosts[i]._id) {

                for (var x = 0; x < obj.account.blogPosts[i].comments.length; x++) {
                    if (commentId == obj.account.blogPosts[i].comments[x]._id) {

                        const index = obj.account.blogPosts[i].comments[x].likes.indexOf(username) 

                        if(obj.account.blogPosts[i].comments[x].likes[index] === username){
                        obj.account.blogPosts[i].comments[x].likes.splice(index, 1);
                        obj.save(err => {
                            if (err) return err
                            res.send('success');
                        });
                        }
                    }
                }

            }
        }

    });
});

那么有没有办法用普通的猫鼬做这两个操作?

1 个答案:

答案 0 :(得分:0)

您可以使用async库或任何callback function来避免callback hell

    app.post('/server-activity/unlike-blog-comment', (req, res) => {
    const username = req.user.account.username;
    const blogAuthor = req.body.blogAuthor;
    const blogId = req.body.blogId;
    const commentId = req.body.commentId;

    userCollection.findOne({
        'account.username': blogAuthor
    }, function(err, obj) {
        if (err) return err;

        async.each(obj.account.blogPosts,function(i,calllback){
           if (blogId == i._id) {
              async.each(i.comments,function(x,callback){
                   if (commentId == x._id) {
                        const index = x.likes.indexOf(username);
                        if(x.likes[index] === username){
                           x.likes.splice(index, 1);
                           obj.save(err => {
                        if (err) return err
                        res.send('success');
                         });
                        }
                   }
               });
           }
        });
   });