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

  • SEARCH
  • Home
  • 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 5992259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:30:21+00:00 2026-05-22T23:30:21+00:00

First to say that I’m new to MongoDb and document oriented db’s in general.

  • 0

First to say that I’m new to MongoDb and document oriented db’s in general.

After some trouble with embedded documents in mongodb (unable to select only nested document (example single comment in blog post)),
I redesigned the db. Now I have two collections, posts and comments (not the real deal, using blog example for convinience sake).

Example – posts collection document:

Array {

'_id'   :  MongoId,

'title' : 'Something',

'body'  : 'Something awesome'   

}

Example – comments document:

Array {

'_id'    : MongoId,

'postId' : MongoId,

'userId' : MongoId,

'commentId' : 33,

'comment' : 'Punch the punch line!'

}

As you can see, I have multiple comment documents (As I said before, I want to be able to select single comment, and not an array of them).

My plan is this: I want to select single comment from collection using postId and commentId (commentId is unique value only among comments with the same postId).
Oh and commentId needs to be an int, so that I could be able to use that value for calculating next and previous documents, sort of “orderWith” number.

Now I can get a comment like this:

URI: mongo.php?post=4de526b67cdfa94f0f000000&comment=4

Code: $comment = $collection->findOne(array(“postId” => $theObjId, “commentId” => (int)$commentId));

I have a few questions.

  1. Am I doing it right?
  2. What is the best way to generate that kind of commentId?
  3. What is the best way to ensure that commentId is unique among comments with the same postId (upsert?)?
  4. How to deal with concurrent queries?
  • 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-05-22T23:30:22+00:00Added an answer on May 22, 2026 at 11:30 pm

    Am I doing it right?

    This is a really difficult question. Does it work? Does it meet your performance needs, are you comfortable maintaining it?

    MongoDB doesn’t have any notion of “normalization” or the “the one true way”. You model your data in a way that works for you.

    What is the best way to generate that kind of commentId?
    What is the best way to ensure that commentId is unique among comments with the same postId (upsert?)?

    This is really a complex problem. If you want to generate monotonically increasing integers IDs (like auto-increment), then you need a central authority for generating these integers. That doesn’t tend to scale very well.

    The commonly suggested method is to use the the ObjectId/MongoId. That will give you a unique ID.

    However, you really want an integer. So take a look at findAndModify. You can keep a “last_comment_id” on your post and then update it when creating a new comment.

    How to deal with concurrent queries?

    Why would concurrent queries be a problem? Two readers should be able to access the same data.

    Are you worried about concurrent comments being created? Then see the find an modify docs.

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

Sidebar

Related Questions

First of all, I must say that I am quite new to the API
First let me say that I really feel directionless on this question. I am
First, let me say that I'm a complete beginner at Python. I've never learned
Let's say I have 3 point clouds: first that has 3 points {x1,y1,z1}, {x2,y2,z2},
For example: First, say I have a Silverlight app with Windowless=true so that I
First of all, let me say I am very new to rails, have been
Let's say the first N integers divisible by 3 starting with 9. I'm sure
My first instinct is to say each key is an object, and has a
Explanation: Say package A is always built first and then package B. I need
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)

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.