As you may have guessed, I am from the RDBMS background.
I have made a document model in MongoDB. The simple representation would be like:
Collection users
{
"_id": <ObjectId>,
"name": "Simon",
"photo": "https://...jpg"
}
Collection: Posts
{
"_id": <ObjectId>,
"author": ObjectId(<AdIdFromUsers>),
"likes": [ ObjectId(<AdIdFromUsers>), ObjectId(<AdIdFromUsers>) ],
"comments": [
{
"author": ObjectId(<AdIdFromUsers>),
"content": "Hi everyone!",
"likes": [ ObjectId(<AdIdFromUsers>), ObjectId(<AdIdFromUsers>) ],
"replies": [
{
"author": ObjectId(<AdIdFromUsers>),
"content": "Hi everyone!",
"likes": [ ObjectId(<AdIdFromUsers>), ObjectId(<AdIdFromUsers>) ],
}
]
}
]
}
I have read from mongodb docs that it's okay to use Manual Reference in most cases.
But my question here is, How can I optimize this to make hundreds of requests to the database to get information about a post? Like:
- Get the post document first
- Then loop through the comments and replies and get author info
- Loop through likes and get each user
Please feel free to correct me if I'm taking the wrong approach