Вопрос

I am working on a rails app with mongodb and mongomapper. I would like to index few keys in the database. This is first project for me on mongo.

The keys i want to index i can specify like this

User.ensure_index(:email)

as described here

My question is, do i need to call this manually(may be wrapping it in a method) to make the indexing really happening?

And how can i find the list of keys which are having indices?

Это было полезно?

Решение

Here are the answers to my questions

Do i need to call this manually(may be wrapping it in a method) to make the indexing really happening?

Yes, we have to manually call the ensure_index method on Model. So we can wrap that in a method and can call from the console or even a rake task.

def self.create_index
  self.ensure_index(:email)
  self.ensure_index(:first_name)
  self.ensure_index(:last_name)
  true
end

then from console

User.create_index

you can check what keys are indexed using mongo's getIndexes() method like this

mongo                      #=> enter the mongo console
show dbs                   #=> see the list of available dbs
use my_database            #=> switch to your database
db.table_name.getIndexes() #=> replace table_name with your's

and that's it, you can see the list of indices on your table

Thanks!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top