angularjs - 如何处理一对多关系中的嵌套 Mongoose 查询和异步问题?

标签 angularjs mongoose one-to-many mean-stack

我试图从数据库中查询所有帖子,然后获取属于每个帖子的所有评论,并将整个内容发送回前端进行显示。到目前为止,我的策略一直是使用嵌套的 Mongoose 查询(请参阅下面的伪代码和实际代码示例),并且由于异步问题而得到了一些意外的结果。

谁能告诉我哪里出了问题,或者是否有更好的方法来完成我想要完成的任务:

我的架构:

我在 Mongoose 中有三个 Schema:

  1. 用户架构(用户)
  2. PostSchema(发布)
  3. CommentSchema (PostComment)

我只在此处包含了 CommentSchema,以简化我的问题:

var CommentSchema = new mongoose.Schema (
    {
        message: {
            type: String,
            minlength: [2, 'Your comment must be at least 2 characters.'],
            maxlength: [2000, 'Your comment must be less than 2000 characters.'],
            required: [true, 'You cannot submit an empty comment.'],
            trim: true,
        }, // end message field
        userID: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        },
        username: {
            type: String,
        },
        postID: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Post'
        },
    },
    {
        timestamps: true,
    }
);

创建新评论时,帖子的 _id 会记录到评论的 .postID 字段中。

我的伪代码策略:

// query for all posts using a mongoose promise
// run a `for loop` through the array returned posts
    // query my comments collection for any comments pertaining to post[i]
        // attach comments returned to the post[i]
        // push post[i] into a new array (now with comments attached)
        // check if on last run through array
            // res.json the new array back to the front end 
            // On front end just iterate through each post and its contained comments.

但是,当我尝试此策略时,for 循环中的第二个 Mongoose 查询出现了一些异步问题。

我的实际代码示例:

Post.find({}) // first query
    .then(function(allPosts) {
        for (var i = 0; i < allPosts.length; i++) {
            _post = allPosts[i];
            console.log(_post, i);
            PostComment.find({postID: _post._id}) // **nested query
                .then(function(comments) {
                    console.log({
                        post: _post,  // this value will not match the console log above (outside of the query)
                        index_value: i, // this value too will be out of match with the console log above
                        comments: comments,
                    });
                    // attach new comment to allPosts[i]
                    // push post with comment attached to new array
                    // check if on last iteration, if so res.json new array
                })
                .catch(function(err) {
                    console.log(err);
                })
         }
    .catch(function(err) {
        console.log(err);
    }

上面代码示例的问题:

在上面的示例中,在第二个查询中,**嵌套查询i 的值和 _post 不同步当数据从 Mongoose promise (.then)返回时。 for 循环的运行速度快于返回数据的速度。因此,如果我尝试将任何注释附加到父 post 对象 (_post),则该变量已经与 for 循环的进程不同步(_post 现在变为数组中的下一篇文章)。我很难解决这个问题,并从每篇文章中获取我的所有评论,并将其捆绑在一起用于前端。我现在很困惑。

期望的行为:

我想要一个包含我所有帖子的填充列表,并在每个帖子上附加评论,以便在前端更轻松地迭代它们。这样,在前端,所有帖子都会显示在其下方,并带有各自的评论。

结论:

我做错了什么?如何遍历我的所有帖子,并获取每个帖子的所有评论,并使其在 Angular 中的前端显示整洁?我的查询方法是否错误或太“昂贵”?有更好的方法来实现我想要的行为吗?

任何见解或帮助都超越赞赏!我四处寻找,希望能看到另一个这样的问题,并且已经在这个问题上摸索了一段时间了 =)

最佳答案

我想出了如何实现这一点。我确实必须对我的模式进行一些更改。因为 Mongo 不允许连接,所以我们可以使用 populate() Mongoose 方法 ( http://mongoosejs.com/docs/populate.html )。

为了让这个解释简短一些,我想展示更新后的架构,然后展示我如何能够只填充一个级别。本文底部是另一个示例,展示了如何跨多个级别进行填充。

用户架构:

// Setup dependencies:
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

// Setup a schema:
var UserSchema = new Schema (
    {
        username: {
            type: String,
            minlength: [2, 'Username must be at least 2 characters.'],
            maxlength: [20, 'Username must be less than 20 characters.'],
            required: [true, 'Your username cannot be blank.'],
            trim: true,
            unique: true, // username must be unique
            dropDups: true,
        }, // end username field
    },
    {
        timestamps: true,
    }
);

// Instantiate our model and export it:
module.exports = mongoose.model('User', UserSchema);

后架构:

// Setup dependencies:
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

// Setup a schema:
var PostSchema = new Schema (
    {
        message: {
            type: String,
            minlength: [2, 'Your post must be at least 2 characters.'],
            maxlength: [2000, 'Your post must be less than 2000 characters.'],
            required: [true, 'You cannot submit an empty post.'],
            trim: true,
        }, // end message field
        user: {
            type: Schema.Types.ObjectId,
            ref: 'User'
        },
        comments: [{
            type: Schema.Types.ObjectId,
            ref: 'Comment'
        }],
    },
    {
        timestamps: true,
    }
);

// updates userID based upon current session login info:
PostSchema.methods.updateUserID = function(id) {
    this.user = id;
    this.save();
    return true;
};

// adds comment to post's comments array:
PostSchema.methods.addComment = function(commentID) {
    this.comments.push(commentID);
    this.save();
    return true;
};

// Instantiate our model and export it:
module.exports = mongoose.model('Post', PostSchema);

评论架构:

// Setup dependencies:
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

// Setup a schema:
var CommentSchema = new Schema (
    {
        message: {
            type: String,
            minlength: [2, 'Your comment must be at least 2 characters.'],
            maxlength: [2000, 'Your comment must be less than 2000 characters.'],
            required: [true, 'You cannot submit an empty comment.'],
            trim: true,
        }, // end message field
        user: {
            type: Schema.Types.ObjectId,
            ref: 'User'
        },
        _post: {
            type: Schema.Types.ObjectId,
            ref: 'Post'
        },
    },
    {
        timestamps: true,
    }
);

// Assigns user ID to comment when called (uses session info):
CommentSchema.methods.updateUserID = function(id) {
    this.user = id;
    this.save();
    return true;
};

// Assigns post ID to comment when called:
CommentSchema.methods.updatePostID = function(id) {
    this._post = id;
    this.save();
    return true;
};

// Instantiate our model and export it:
module.exports = mongoose.model('Comment', CommentSchema);

解决方案:使用填充方法:

使用 populate() 方法 ( http://mongoosejs.com/docs/populate.html ),我们可以从 Post 模型开始并填充 comments 字段。

使用 PostSchema 中定义的实例方法,我们将评论 ID 推送到 Post.comments 数组中,下面的 populate() 方法会获取所有评论对象并替换该数组具有实际评论对象的 ID。

var User = require('mongoose').model('User');
var Post = require('mongoose').model('Post');
var PostComment = require('mongoose').model('Comment');

Post.find({})
    .populate('comments')  // populates comments objects based on ids in `comments`
    .exec()
    .then(function(commentsAndPosts) {
        console.log(commentsAndPosts);
        return res.json(commentsAndPosts);
    })
    .catch(function(err) {
        console.log(err);
        return res.json(err);
    })

提供给 Mongoose 文档的链接有一个很好的干净示例。

现在,在前端,每​​个帖子对象内部都有一个评论数组,并且所有评论对象都已填充!甜甜的!

概述:

我能够在每个 Post 对象中存储评论 ID 数组(当发表新评论时,comment._id 会被推送到 Post.comments大批)。使用populate(),我们可以查询 Comments 集合并获取与相关 ID 关联的所有评论对象。这很棒,因为在我们的 populate 方法完成后,我们可以将所有帖子和评论的整个数组作为一个 JSON 对象发回,并在前端迭代它们。

在多个级别填充:

假设在上述场景中,我还想获取帖子和评论作者的用户名(注意:每个评论和帖子对象都有一个 user 字段,其中存储 User. _id

我们可以使用 populate,通过嵌套一些参数来跨越多个级别,如下所示。这将提供与上面的示例相同的数据(所有帖子和所有评论),但包括基于存储的用户 ID 的评论和帖子的用户名:

Post.find({}) // finds all posts
    .populate('user') // populates the user (based on ID) and returns user object (entire user object)
    .populate({ 
        path: 'comments', // populates all comments based on comment ID's
        populate: {  path: 'user' } // populates all 'user" fields for all comments (again, based on user ID)
    })
    .exec()
    .then(function(commentsAndPostsWithUsers) {
        console.log(commentsAndPostsWithUsers);
        return res.json(commentsAndPostsWithUsers);
    })
    .catch(function(err) {
        console.log(err);
    })

在上面的示例中,我们首先获取所有帖子,然后获取每个帖子的所有用户对象,然后获取所有评论,以及每个评论的每个用户对象,并将其全部捆绑起来!

使用 Angular 在前端进行迭代:

我们可以使用 ng-repeat** 迭代返回的帖子:

<!-- Repeating Posts -->
<div ng-repeat="post in commentsAndPostsWithUsers >
    <h3 ng-bind="post.user.username"></h3>
    <h2 ng-bind="post.createdAt"></h2>
    <p ng-bind="post.message"></p>
    <!-- Repeating Comments -->
    <div ng-repeat="comment in post.comments">
        <h4 ng-bind="comment.user.username"></h4>
        <h5 ng-bind="comment.createdAt"></h5>
        <p ng-bind="comment.message"></p>
    </div>
</div>

** 上面的代码示例中未显示 Angular Controller 。

我们可以分别通过 post.user.usernamepost.user.username 访问帖子或评论的用户名,因为整个用户对象已附加(这就是为什么我们必须向下导航才能获取用户名)。然后,我们可以使用另一个 ng-repeat 来迭代 post.comments,显示所有评论。祝一切顺利,并希望这可以帮助人们避免我所做的困惑!

关于angularjs - 如何处理一对多关系中的嵌套 Mongoose 查询和异步问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42000038/

相关文章:

javascript - AngularJS - 使用指令调用回调函数?

angularjs - Amazon S3浏览器直接上传唯一文件名

node.js - $group by 之后的动态键

php - 拉维尔 4.2 : How to use whereIn() and with() to get a list of items in a one-to-many relationship?

javascript - 在 Protractor 中捕获 “angular could not be found on the window”

javascript - 如何在 ui-grid 中使用刷新方法?

mongodb - 如何处理MongoDB中的多对多关系?

Mongodb - 如何在查找查询中使用聚合文本搜索

java - 与 Collection<Integer> 的一对多

sql - 如何在 SQLITE3 中创建一对多?