我在Rails 4.0.3应用程序中有一个模型,它使用Mongoid4(直接来自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)在多个字段上创建索引?我是不是漏了什么东西?
发布于 2014-03-12 14:26:04
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。
希望这能有所帮助。
https://stackoverflow.com/questions/22350335
复制相似问题