Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Mongo Basics Working With Collections Managing Collections

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

Why is one "_id" and the other "_id_"?

> db.posts.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "mongoBasics.posts"
    }
]

And then when I try and delete _ id _ it refers to it without the 2nd underscore as _ id:

> db.posts.dropIndex('_id_');
{
    "nIndexesWas" : 1,
    "ok" : 0,
    "errmsg" : "cannot drop _id index",
    "code" : 72
}

What's this underscore business?

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Brendan;

Great question. As you learned, MongoDB automatically generates a field for us called _id. By default it is assigned a type of ObjectId, which is a 12-byte BSON type. Indexes are vital to performance in MongoDB and the database automatically generates an index on the _id field and assigns the index a name _id_. This is a built in index and cannot be dropped, as it sounds like you discovered. Further it's name cannot be altered because there currently is not a way to modify an index in MongoDB, they have to be dropped and recreated.

Post back if you have further questions.

Happy coding,
Ken

Ken Alger
Ken Alger
Treehouse Teacher

_id is the field name. _id_ is the name of the index.

Why are they named that way... because that is what the developers at MongoDB decided. :smile:

Happy coding,
Ken