开发者

MongoDB (PHP) - Custom "id", and OrderWith number

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 p开发者_Python百科lan 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?


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.


I don't know if The Big Picture will allow you to do this, but here is how I'd do it.

I'd have an array of comments contained inside each post. This means no joins are needed. In your case, normalization of comments doesn't give any benefit. I'd replace CommentID with CreatedAt as the time of creation.

This will let you have an easy data model to work with, as well as the ability to sort it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜