如何有效地获取 MongoDB/mongoose 帖子的评论?
How to effectively fetch comments for posts with MongoDB/mongoose?
我有以下 Post 和 Collection 个文件:
// posts
{
"_id" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Post body 1",
"created_at" : 1452772748737
},
{
"_id" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Post body 2",
"created_at" : 1452772759731
}
// comments
{
"_post" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Comment 1"
},
{
"_post" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Comment 2"
}
我需要查询所有 post 的所有评论,结果如下:
{
"_id" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Post body 1",
"created_at" : 1452772748737,
"comments": [{
"_post" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Comment 1"
}]
},
{
"_id" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Post body 2",
"created_at" : 1452772759731,
"comments": [{
"_post" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Comment 2"
}]
}
我的 post 和 collections 架构如下所示:
// post
var PostSchema = mongoose.Schema({
},{
strict: "throw",
collection: "posts"
});
PostSchema.add({
created_at: {
type: Number,
'default': Date.now
}
});
PostSchema.add({
title: {
type: String
}
});
PostSchema.add({
body: {
type: String
}
});
// comment
var CommentSchema = mongoose.Schema({
},{
strict: "throw",
collection: "comments"
});
CommentSchema.add({
_post: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Post'
}
});
CommentSchema.add({
body: {
type: String
}
});
获得上述结果的有效方法是什么?
尝试通过mongoose populate and aggregate完成。示例代码如下。
var Post = mongoose.model('Post', PostSchema);
var Comment = mongoose.model('Comment', CommentSchema);
Comment.aggregate([
{$group: {_id: '$_post', comments: {$push: '$body'}}}
// ...
], function(err, result) {
if (err)
// error handling
Post.populate(result, {path: "_id"}, function(err, ret) {
if(err)
console.log(err);
else
console.log(ret);
});
});
我有以下 Post 和 Collection 个文件:
// posts
{
"_id" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Post body 1",
"created_at" : 1452772748737
},
{
"_id" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Post body 2",
"created_at" : 1452772759731
}
// comments
{
"_post" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Comment 1"
},
{
"_post" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Comment 2"
}
我需要查询所有 post 的所有评论,结果如下:
{
"_id" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Post body 1",
"created_at" : 1452772748737,
"comments": [{
"_post" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Comment 1"
}]
},
{
"_id" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Post body 2",
"created_at" : 1452772759731,
"comments": [{
"_post" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Comment 2"
}]
}
我的 post 和 collections 架构如下所示:
// post
var PostSchema = mongoose.Schema({
},{
strict: "throw",
collection: "posts"
});
PostSchema.add({
created_at: {
type: Number,
'default': Date.now
}
});
PostSchema.add({
title: {
type: String
}
});
PostSchema.add({
body: {
type: String
}
});
// comment
var CommentSchema = mongoose.Schema({
},{
strict: "throw",
collection: "comments"
});
CommentSchema.add({
_post: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Post'
}
});
CommentSchema.add({
body: {
type: String
}
});
获得上述结果的有效方法是什么?
尝试通过mongoose populate and aggregate完成。示例代码如下。
var Post = mongoose.model('Post', PostSchema);
var Comment = mongoose.model('Comment', CommentSchema);
Comment.aggregate([
{$group: {_id: '$_post', comments: {$push: '$body'}}}
// ...
], function(err, result) {
if (err)
// error handling
Post.populate(result, {path: "_id"}, function(err, ret) {
if(err)
console.log(err);
else
console.log(ret);
});
});