首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB/Mongoid:有可能在多个字段上同时具有唯一和dropDups选项的索引吗?

MongoDB/Mongoid:有可能在多个字段上同时具有唯一和dropDups选项的索引吗?
EN

Stack Overflow用户
提问于 2014-03-12 11:39:14
回答 1查看 1.4K关注 0票数 0

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

代码语言:javascript
复制
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'})

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

代码语言:javascript
复制
 rake db:mongoid:create_indexes

我知道这个错误:

代码语言:javascript
复制
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)在多个字段上创建索引?我是不是漏了什么东西?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-12 14:26:04

dropDups不是mongoid的有效索引选项。您需要使用drop_dups代替。

代码语言:javascript
复制
 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

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22350335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档