Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8048307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:23:29+00:00 2026-06-05T06:23:29+00:00

I cant find best solution for very simple problem(or not very) Have classical set

  • 0

I cant find “best” solution for very simple problem(or not very)

Have classical set of data: posts that attached to users, comments that attached to post and to user.

Now i can’t decide how to build scheme/classes

On way is to store user_id inside comments and inside.
But what happens when i have 200 comments on page?
Or when i have N posts on page?
I mean it should be 200 additional requests to database to display user info(such as name,avatar)

Another solution is to embed user data into each comment and each post.

But first -> it is huge overhead, second -> model system is getting corrupted(using mongoalchemy), third-> user can change his info(like avatar). And what then? As i understand update operation on huge collections of comments or posts is not simple operation…

What would you suggest? Is 200 requests per page to mongodb is OK(must aim for performance)?

Or may be I am just missing something…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:23:30+00:00Added an answer on June 5, 2026 at 6:23 am

    You can avoid the N+1-problem of hundreds of requests using $in-queries. Consider this:

    Post {
      PosterId: ObjectId
      Text: string
      Comments: [ObjectId, ObjectId, ...] // option 1
    }
    
    Comment {
      PostId: ObjectId // option 2 (better)
      Created: dateTime,
      AuthorName: string,
      AuthorId: ObjectId,
      Text: string
    }
    

    Now you can find the posts comments with an $in query, and you can also easily find all comments made by a specific author.

    Of course, you could also store the comments as an embedded array in post, and perform an $in query on the user information when you fetch the comments. That way, you don’t need to de-normalize user names and still don’t need hundreds of queries.

    If you choose to denormalize the user names, you will have to update all comments ever made by that user when a user changes e.g. his name. On the other hand, if such operations don’t occur very often, it shouldn’t be a big deal. Or maybe it’s even better to store the name the user had when he made the comment, depending your requirements.

    A general problem with embedding is that different writers will write to the same object, so you will have to use the atomic modifiers (such as $push). This is sometimes harder to use with mappers (I don’t know mongoalchemy though), and generally less flexible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have (what I feel to be) a very simple scenario that I can't
I searched a lot over Google, but cant find a valid solution for best
Sometimes I come across this problem where you have a set of functions that
but cant find any information. Restricted on time. I have a Datagridview, 6 fields.
similar questions have been asked before but I cant find an exact match to
This seems like a very simple and a very common problem. The simplest example
I need to find the best or native resolution for an attached LCD monitor
I believe the answer may be quite simple but i cant find the answer
Yet another multi tenancy post im afraid. I just cant find a good solution
This will probably be obvious but I can't find the best way. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.