【问题标题】:MongoDB and Rails: How to create IndexMongoDB 和 Rails:如何创建索引
【发布时间】:2012-01-07 23:15:21
【问题描述】:

我在初始化程序 mongo_config.rb 中寻找一种方法来为位置创建索引。也就是说,我希望能够做到

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

在初始化程序和 ruby​​ 中。我该怎么做?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 mongodb


    【解决方案1】:

    MongoDB Ruby Documentation。你想要ensure_index,例如map.ensure_index([["gps", Mongo::GEO2D]]) 会做你想做的事,假设mapMongo::Collection

    【讨论】:

    • 如果您使用 MongoMapper,ensure_index 也可用作类方法,因此 Map.ensure_index([["gps", Mongo::GEO2D]]) 应该可以工作。
    【解决方案2】:

    更新示例:map.indexes.create_one({some_key: 1}, {unique: true})(还假设 mapMongo::Collection

    【讨论】:

      猜你喜欢
      • 2015-02-06
      • 2011-03-20
      • 2017-04-13
      • 2014-09-01
      • 2021-08-24
      • 2018-10-22
      • 2012-04-01
      • 1970-01-01
      • 2020-10-08
      相关资源
      最近更新 更多