0

我在 Rails 4.0.3 应用程序中有一个模型,它使用 Mongoid 4(直接来自 GitHub 的主分支),我试图确保多个字段上的索引是唯一的并删除重复项。

class MyModel
  include Mongoid::Document  

  field :a, type: Integer
  field :b, type: Integer

  index({a: 1, b: 1}, {unique: true, dropDups: true, name: 'unique_drop_dups_idx'})

但是当我运行命令来创建索引时:

 rake db:mongoid:create_indexes

我收到此错误:

Problem:
  Invalid index specification on MyModel: {:a=>1, :b=>1}, {:unique=>true, :dropDups=>true, :name=>"unique_drop_dups_idx"}
Summary:
  Indexes in Mongoid are defined as a hash of field name and direction/2d pairs, with a hash for any additional options.
Resolution:
  Ensure that the index conforms to the correct syntax and has the correct options.

如果我摆脱该dropDups选项,则索引创建开始,即使由于存在重复项而最终失败。

错误消息是否意味着无法使用此配置 ( unique+ dropDups) 在多个字段上创建索引?我还缺少其他东西吗?

4

1 回答 1

1

dropDups不是 mongoid 的有效索引选项。你需要drop_dups改用。

 index({a: 1, b: 1}, {unique: true, drop_dups: true, name: 'unique_drop_dups_idx'})

我们尽量不使用骆驼案作为选择,因为我们在红宝石土地上。您可以在这里查看映射https://github.com/mongoid/mongoid/blob/master/lib/mongoid/indexable/specification.rb#L14

希望有帮助。

于 2014-03-12T14:26:04.933 回答