mongoid 是否自动索引字段_id?

Is mongoid automatically indexing the field _id?

我第一次使用 noSQL 数据库,使用 mongodb。我正在使用 Ruby-2.2.2 与 Rails-3.2.22 和 mongoid-3.17

我看到 Mongoid 自动在模型中包含一个 _id 字段。

但是,

我是否需要为该字段手动创建索引,或者它是自动创建的,就像关系数据库中的 ActiveRecord 一样?

如何查看每个模型存在的索引列表?

要获取现有索引的列表,您应该在 mongodb 控制台(mongo 本地主机上的二进制文件)中询问:

use databasename;
db.collectionname.getIndexes();

其中 databasename 是数据库名称,collectionname 是相应的集合名称。 http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/

是的,mongodb 自动为 _id 字段建立了索引。 http://docs.mongodb.org/manual/core/index-single/#single-field-indexes