如何使用grails和mongodb创建一个唯一的索引?

时间:2014-07-11 07:49:39

标签: mongodb grails gorm-mongodb

class Country{
    String id
    String code
    Set<State> states
    static embedded = ['states']
}
class State{
    String id
    String code
}

我尝试为国家/地区代码+州代码设置唯一索引(或规范约束验证)

这些不起作用:

  • 代码唯一:约束中的true
  • code index:true,indexAttributes:[unique:true]

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

class State{
    String id
    String code
    static belongsTo = [country: Country]
    static constraints = {
        code( unique: ['country'])
    }
}