【问题标题】:mongoose - add and remove object from 3 level deep arraymongoose - 从 3 级深度数组中添加和删除对象
【发布时间】:2023-04-11 09:45:02
【问题描述】:

您好,我有这样的架构

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

我想要做的是当用户喜欢 blogPost 上的评论时,我想将他们的用户名推送到 likes 数组。首先,我必须找到帖子以访问与该帖子相关的 cmets。然后我必须找到他们喜欢的评论并将那里的名称推送到 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');
                        });
                        }
                    }
                }

            }
        }

    });
});

按预期工作,但它很混乱我想知道是否有办法只用猫鼬做到这一点?

对于不喜欢评论,它的工作方式几乎相同,除了我从下面的 likes 数组中拼接用户

    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');
                        });
                        }
                    }
                }

            }
        }

    });
});

那么有没有办法只用普通的猫鼬来完成这两个操作?

【问题讨论】:

    标签: arrays mongodb mongoose nested


    【解决方案1】:

    您可以使用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');
                             });
                            }
                       }
                   });
               }
            });
       });
    

    【讨论】:

    • 非常感谢!不完全是我想要的,但仍然比我拥有的更好,我会接受它
    猜你喜欢
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 2021-07-27
    • 2020-10-30
    • 2021-07-22
    • 2018-01-13
    • 1970-01-01
    相关资源
    最近更新 更多