2012-01-07 50 views
2

我正在尋找一種方法在初始化程序mongo_config.rb中爲位置創建索引。換句話說,我希望能夠做到MongoDB和Rails:如何創建索引

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

在初始化和紅寶石。我該怎麼做呢?

回答

4

參見MongoDB Ruby Documentation。你想ensure_index,例如map.ensure_index([["gps", Mongo::GEO2D]])會做你想做的,假設mapMongo::Collection

+0

'ensure_index'也可作爲一個類的方法,如果你使用MongoMapper所以'Map.ensure_index([ 「GPS」,蒙戈:: GEO2D]])'應該工作。 – 2012-01-07 23:46:40

0

更新例如:map.indexes.create_one({some_key: 1}, {unique: true})(也假設mapMongo::Collection