如何使用MongoDB和Mongoose为Node.js存储线程注释?

mvb*_*fst 3 comments mongoose mongodb node.js

在MongoDB和Mongoose中存储注释树的最佳方法是什么?目前我有这个:

CommentableSchema = new Schema({
    ...
});

ReplySchema = new Schema({
    userId: Number,
    'body': String,
    createdAt: Date,
    updatedAt: Date
});

CommentSchema = new Schema({
    commentable: CommentableSchema,
    userId: Number, // users are NOT stored in MongoDB
    subject: String,
    'body': String,
    createdAt: Date,
    updatedAt: Date,
    replies: [ReplySchema],
    length: Number // comment + all replies
});
Run Code Online (Sandbox Code Playgroud)

但这似乎只适用于顶级评论+ 1级评论.我相当肯定我不能ReplySchema在里面使用ReplySchema.

kro*_*roe 6

从官方手册中,我猜你会在这里找到比你所有答案更多的答案(:

http://docs.mongodb.org/manual/tutorial/model-tree-structures/

编辑:你也可以使用mongoose的"populate"函数:http://mongoosejs.com/docs/populate.html

祝好运