Yogesh Prajapati
Yogesh Prajapati

Reputation: 4870

MongoDb need id to each element

here is the json representation of one mongodb record

{
  _id : ObjectId("4e77bb3b8a3e000000004f7a"),
  when : Date("2011-09-19T02:10:11.3Z",
  author : "alex",
  title : "No Free Lunch",
  text : "This is the text of the post.  It could be very long.",
  tags : [ "business", "ramblings" ],
  votes : 5,
  voters : [ "jane", "joe", "spencer", "phyllis", "li" ],
  comments : [
    { who : "jane", when : Date("2011-09-19T04:00:10.112Z"),
      comment : "I agree." },
    { who : "meghan", when : Date("2011-09-20T14:36:06.958Z"),
      comment : "You must be joking.  etc etc ..." }
  ]
}

now i have a problem that if i want to delete any of comment from comments array than how can i do that ?

i don't want to rely on comment text.

is there any facility that give id to each and every object in document.

Upvotes: 2

Views: 1391

Answers (1)

muruga
muruga

Reputation: 1073

_id field will be automatically added to the top level documents when not populated during the document insertion. When you have sub-documents, it is based on your application need and you have to design such that, you should be able to point that sub-document by some means.

For your case, we can probably use the comment_id field which can be used to identify your sub-document uniquely for various operations on them later (if you need to).

Hope this helps!

Upvotes: 1

Related Questions