如何使用MongoDB和Mongoose为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 });
但是,这似乎只适合顶级评论+ 1级的评论。 我相当肯定,我不能在ReplySchema
使用ReplySchema
。
你可以看看这些链接:
http://groups.google.com/group/mongodb-user/browse_thread/thread/3f35c3fb28891b52
http://blog.fiesta.cc/post/11319522700/walkthrough-mongodb-data-modeling
另外,您确定recursion架构不受支持吗? 你可以检查这个链接:
Mongoose:Coffeescript中的recursionembedded式文档
从官方手册,我想你会发现在这里比你所有的答案(:
http://docs.mongodb.org/manual/tutorial/model-tree-structures/
编辑:你也可以使用mongoose的“填充”function: http : //mongoosejs.com/docs/populate.html
祝你好运