Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k 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 (106k 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 (107k 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
asked Feb 11, 2020 in Web Technology by ashely (50.2k points)
0 votes
2 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...