mongoose - 从 3 级深度数组中添加和删除对象

Posted

技术标签:

【中文标题】mongoose - 从 3 级深度数组中添加和删除对象【英文标题】:mongoose - add and remove object from 3 level deep array 【发布时间】:2018-08-04 15:21: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');
                        );
                        
                    
                

            
        

    );
);

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

【问题讨论】:

【参考方案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');
                         );
                        
                   
               );
           
        );
   );

【讨论】:

非常感谢!不完全是我想要的,但仍然比我拥有的更好,我会接受它

以上是关于mongoose - 从 3 级深度数组中添加和删除对象的主要内容,如果未能解决你的问题,请参考以下文章

php 添加和删​​除数组

Mongoose 深度填充多个 2 级对象不起作用

如何从深度嵌套的文档中 $pull (mongoose)

从 Mongoose Find() 查询向对象数组添加属性 [重复]

从 Mongoose Find() 查询向对象数组添加属性 [重复]

添加和删​​除时DbContext很慢