Reputation: 1323
in mongo
console, I can execute db.collection.getIndexes()
to view all the indices defined for a particular collection.
What is the mongoid
equivalent of the above query?
Taking this reference
I tried:
CollectionName.collection.getIndexes()
but it has no method as such. It seems like you can perform the above query in mongoid 3.0
but not in mongoid 2.4
.
Upvotes: 0
Views: 2009
Reputation: 434665
If CollectionName
is your model then CollectionName.collection
will be a Moped::Collection
. To get at a collection's indexes from a Moped::Collection
you use the indexes
method:
indexes = CollectionName.collection.indexes
Then you can use each
to look at the indexes:
CollectionName.collection.indexes.each do |index|
# index['key'] has the keys as a Hash
# index['unique'] will be true for a unique index
# ...
end
The Mongoid API and Moped API might be worth a review.
If you're in Mongoid2 land, then I think you'd have to use the standard low level MongoDB interface rather than Moped. In that case CollectionName.collection.driver
would be a Mongo::Collection
and the index methods would be:
create_index
and ensure_index
for creating indexes.drop_index
for dropping an index.drop_indexes
for dropping all indexes at once.index_information
for getting information about a collection's indexes.In your case, you'd want to look at the CollectionName.collection.driver.index_information
Hash.
Upvotes: 2
Reputation: 1323
Finally came across the command which fetches the indexes:
CollectionName.collection.index_information
But I wonder, why the above command is not mentioned anywhere in the documentation?
Upvotes: 1