I have the following post documents:
{
"_id" : ObjectId("56960cd909b0d8801d145543"),
"title" : "Post title",
"body" : "Post body"
}
{
"_id" : ObjectId("56960cd909b0d8801d145544"),
"_post": ObjectId("56960cd909b0d8801d145543"),
"body" : "Comment one"
}
{
"_id" : ObjectId("56960cd909b0d8801d145544"),
"_post": ObjectId("56960cd909b0d8801d145543"),
"body" : "Comment Two"
}
As you can see from the my documents above this is flat list of my post and comment implementation (like SO). If post has _post field then it's comment but if no it's post itself.
When I query for question 56960cd909b0d8801d145543 I need to get response from mongoDB in the following view:
// query
Post.aggregate({_id: ObjectId("56960cd909b0d8801d145543")});
// result
{
"_id" : ObjectId("56960cd909b0d8801d145543"),
"title" : "Post title",
"body" : "Post body",
"comments" [{
"_id" : ObjectId("56960cd909b0d8801d145544"),
"_post": ObjectId("56960cd909b0d8801d145543"),
"body" : "Comment one"
},
{
"_id" : ObjectId("56960cd909b0d8801d145544"),
"_post": ObjectId("56960cd909b0d8801d145543"),
"body" : "Comment Two"
}]
}
How shoul I construct aggregation pipilenes to get result above?