1
votes

I have an application where I use spring framework and I use mongoDB as database. I have a collection which is indexed by email. The fields are:

{
    email: <email>
    profile: {
        name: <name>
        age: <age>
        customMap: {
        }
    }
}

This is the document:

@Document(collection = "user")
class User {
    String email;
    Profile profile;
}

class Profile {
    String name;
    int age;
    Map customMap;
}

The use case is that email field is unique. So when a request comes in with an existing email, I need to update the document with new profile. I'm able to insert a new document with mongoOperations.save call. But while doing an update, if I pass customProfile with new map, I get

java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null

As an error. But if I try to update only age field, it updates successfully. I'm facing problem only with using a map and I'm using a HashMap.

This is the code for update:

Update update = new Update();
Profile profile = new Profile();
profile.setAge(10);
Map customMap = new HashMap();
customMap.put("name", "value");
profile.setCustomMap(customMap);
update.set("profile", profile);
mongoOperations.updateFirst(query, update, User.class);

If I comment out profile.setCustomMap(customMap); then it updates successfully. Am I missing anything here?

1
stackoverflow.com/questions/3580529/… Seems this could be helpful. - Arun A K
@ArunAK In the above example they're not using spring. The only problem I'm facing is when I introduce a map. - coder

1 Answers

0
votes

Make sure to set the email in your User object. Otherwise the DB won't know which User to update.