Is mongoid automatically indexing the field _id?

331 views Asked by At

I am on my first experience with noSQL databases, using mongodb. I am using Ruby-2.2.2 with Rails-3.2.22 and mongoid-3.17

I've seen that the Mongoid automatically includes an _id field in the models.

BUT,

Do I need to manually create an index for this field or it is automatically created as it happens with ActiveRecord in relational databases?

How can I see the list of indexes that exists for each model?

1

There are 1 answers

0
aratak On BEST ANSWER

To get list of existing indexes you should ask inside mongodb console (mongo binary on the localhost):

use databasename;
db.collectionname.getIndexes();

Where databasename is database name and collectionname is the collection name accordingly. http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/

And yes, mongodb has indexes for _id fields automatically. http://docs.mongodb.org/manual/core/index-single/#single-field-indexes