denniss
denniss

Reputation: 17629

MongoDB and Rails: How to create Index

I was looking for a way in the initializer mongo_config.rb to create an index for locations. In other words, I want to be able to do

db.map.ensureIndex({"gps" : "2d"})

in the initializer and in ruby. How do I do this?

Upvotes: 2

Views: 3370

Answers (2)

Tony
Tony

Reputation: 19181

Updated example: map.indexes.create_one({some_key: 1}, {unique: true}) (also assuming map is a Mongo::Collection)

Upvotes: 2

Zachary Anker
Zachary Anker

Reputation: 4520

See MongoDB Ruby Documentation. You want ensure_index, for example map.ensure_index([["gps", Mongo::GEO2D]]) will do what you want, assuming map is a Mongo::Collection.

Upvotes: 5

Related Questions