16

Why is there an underscore in front of the MongoDb document id ?

Why not call it "id" instead of "_id" ?

Is this part of a naming convention I'm not aware of ?

1
  • 2
    Not as far as I know but sometimes you put a _ in front of variables to denote a private or read only attribute of a class, even though _id is settable in insert it is read only every other time, just a wild guess there but a adds up
    – Sammaye
    Mar 18, 2013 at 15:45

1 Answer 1

21

You may notice that MongoDB has a lot of functions (in the shell) and fields that start with an understore as a designation that they are internal or special and not user provided.

If you have your own "ID" then you can store it as "id" and still use the provided-by-MongoDB _id field although you are not required to use the ObjectId that MongoDB generates - you could store your own natural primary key in the "_id" field which will have a unique index on it always.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.