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

How do I deal with nested mongoose queries and async issues in my one-to-many relationship?

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

任何人都可以告诉我哪里出错了,或者是否有更好的方法来完成我想要完成的事情:

我的模式:

我在 Mongoose 中有三个 Schemas

  1. 用户架构(User
  2. PostSchema (Post)
  3. 评论架构(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,
    }
);

创建新评论时,post 的 _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);
    }

上述代码示例中的问题:

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

期望的行为:

我想要一个包含我所有 post 的填充列表,每个 post 都附有注释,以便在前端更轻松地迭代它们。这样一来,在前端,所有 post 都会在下方显示各自的评论。

结论:

我做错了什么?我怎样才能遍历我所有的 post,并获取每个评论的所有评论,并使其在 Angular 中的前端显示整齐?在我的查询中我的方法是错误的还是 "costly" ?有没有更好的方法来实现我想要的行为?

任何见解或帮助都超越感激不尽!我四处搜索,希望能看到另一个类似的问题,并且一直在思考这个问题 =)

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

为了简短起见,我想展示更新后的模式,然后展示我如何能够只填充一个级别。此 post 的底部是另一个示例,展示了如何跨多个级别填充。

用户架构:

// 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);

Post架构:

// 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 文档的 link 有一个很好的干净示例。

现在在前端,每​​个post对象里面都有一个comments数组,所有的comment对象都已经填充好了!甜!

概览:

我能够在每个 Post 对象中存储一个评论 ID 数组(当创建新评论时,comment._id 被推入 Post.comments 数组)。使用 populate(),我们可以查询 Comments 集合并获取与相关 ID 关联的所有评论对象。这很棒,因为在我们的填充方法完成后,我们可以将所有 post 和评论的整个数组作为一个 JSON 对象发回,并在 front-end 上迭代它们。

在多个级别填充:

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

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

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

在上面的示例中,我们首先抓取所有 post,然后抓取每个 post 的所有用户对象,然后抓取所有评论,以及每个评论的每个用户对象,并将其捆绑在一起!

迭代 Front-End 与 Angular:

我们可以使用 ng-repeat**:

遍历返回的 posts
<!-- 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 上面的代码示例中没有显示控制器。

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