Please consider following data model where it is designed to put multiple types of documents into single sharded MongoDB collection or container.
{
"id": "<post-id>",
"type": "post",
"postId": "<post-id>",
"userId": "<post-author-id>",
"title": "<post-title>",
"content": "<post-content>",
"creationDate": "<post-creation-date>"
}
{
"id": "<comment-id>",
"type": "comment",
"postId": "<post-id>",
"userId": "<comment-author-id>",
"content": "<comment-content>",
"creationDate": "<comment-creation-date>"
}
{
"id": "<like-id>",
"type": "like",
"postId": "<post-id>",
"userId": "<liker-id>",
"creationDate": "<like-creation-date>"
}
We have requirement to combine / join these documents types to provide following output.
Example Input -
{
"id": "post-id",
"type": "post",
"postId": "post-id",
"userId": "post-author-id-1",
"title": "post-title",
"content": "post-content"
}
{
"id": "comment-id-1",
"type": "comment",
"postId": "post-id",
"userId": "comment-author-id-1",
"content": "comment-content-1"
}
{
"id": "comment-id-2",
"type": "comment",
"postId": "post-id",
"userId": "comment-author-id-2",
"content": "comment-content-2"
}
{
"id": "comment-id-3",
"type": "comment",
"postId": "post-id",
"userId": "comment-author-id-3",
"content": "comment-content-"
}
Example Output -
{
"id": "post-id",
"type": "post",
"postId": "post-id",
"userId": "post-author-id-1",
"title": "post-title",
"content": "post-content"
"comments": [
{
"id": "comment-id-1", ….
} ,
{
"id": "comment-id-2", ….
},
{
"id": "comment-id-3", ….
}
],
"totalCommentCount": 3
}
Is it possible to do above transformation efficiently in single query or aggregation pipeline in MongoDB? I would also like to compare single container having different types and having separate container for each type data modeling, What are the pros and cons, will there be scalability concerns in Sharding?