jkuehn / gorm-mongodb

Grails GORM implementation for the MongoDB document oriented database
http://www.grails.org/plugin/mongodb-morphia
Apache License 2.0
21 stars 14 forks source link

did it support "unique" save? #4

Closed lsx closed 13 years ago

lsx commented 13 years ago

hi, I use this plugin now. did it support unique field save only one time? like this: class User{ String name; static constraints = { name(blank: false, unique: true) } } def user1=new User("myname"); def user2=new User("myname"); user1.save(); user2.save();// the unique field "name" say it can't save, but now it can save, use db.User.find(), show same name, different ObjectId.

jkuehn commented 13 years ago

Currently you can make fields unique by using indexes only. See http://jkuehn.github.com/gorm-mongodb/ref/Indexes/indexes.html

class User {
    String name;
    static constraints = {
        name(blank: false)
    }
    static indexes = {
        idx_name unique:true, fields:['name']
    }
}

def user1=new User("myname");
def user2=new User("myname");
user1.save();
user2.save(); // throws an Exception - i guess MongoException.DuplicateKey
lsx commented 13 years ago

Thanks a lot! it's works good now.