1

I have a collection of posts

{
  "uid": ObjectId("57e58f6a1ccbdd1407000029"),
  "text": "Post content goes here!",
  "comments": [
    {
      "_id": // unique ID
      "comment": "Comment 1",
    },
    {
      "_id": // unique ID
      "comment": "Comment 2",
    }
  ]
}

The above is a simple example of the post collection's document structure.

I was thinking that, what would be the best way to generate and store a unique id (using PHP) for each comment so that it could be selected specifically to edit or delete.

John
  • 142
  • 9
  • 1
    As a rule of thumb: If you need a unique ID, a subdue is probably a bad Idea and you should use a dedicated collection. Please see http://stackoverflow.com/questions/32409635/mongodb-query-comments-along-with-user-information/32411030#32411030 for details. – Markus W Mahlberg Nov 02 '16 at 00:31

1 Answers1

1

I agree with Markus' comment, but to answer the question you should consider

  1. generating BSON ObjectId which is pretty much unique and doesn't require anything additional
  2. generating UUID using for instance ramsey/uuid
Community
  • 1
  • 1
malarzm
  • 2,831
  • 2
  • 15
  • 25