0 votes
1 view
in Web Technology by (47.8k points)

Is there any difference between using the field ID or _ID from a MongoDB document?

I am asking this because I usually use "_id", however, I saw this sort({id:-1}) in the documentation:

http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingObjectIDs-Sortbyidtosortbyinsertiontime

2 Answers

0 votes
by (107k points)

The difference can be understood by following, The _id field is the primary key for every document. It's called _id and is also accessible via id. Attempting to use an id key may result in an illegal ObjectId format error. 

0 votes
by (80.9k points)

The _id field in MongoDB document database is considered to be the default field for BSON ObjectId's and it is, by default, indexed. _id and id are not the same. You may also prefer to add a field called id if you want, but it will not be index unless you add an index.

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...