2

I'm trying to create an embedded collection in a Grails GORM Mongo domain class.

class User {
    String name
    Set<String> friends = []
}

I want to store a Set (a non-duplicated list) of other names of users.

When I try to save the User domain class:

new User(name: 'Bob').save(failOnError: true)

I get the error.

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for interface java.util.Set.

Changing the Set to List works fine but I don't want duplicates and don't want to have to manage that with a List.

Is there a way GORM will use the underlying Mongo $addToSet functionality.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
hugheba
  • 33
  • 1
  • 5

1 Answers1

0

It might be a GORM MongoDB issue. You can create an issue here with reproducing the issue.

But for now, you can do the workaround this problem using the List like this:

class User {

    String name
    List<String> friends = []

    void removeDuplicate() {
        this.friends?.unique()
    }


    def beforeInsert() {
         removeDuplicate()
    }

    def beforeUpdate() {
         removeDuplicate()
    }
}
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121