我在 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
) 在多个字段上创建索引?我还缺少其他东西吗?