Java mongoDB中的更新

Java mongoDB中的更新,java,spring,mongodb,spring-data-mongodb,Java,Spring,Mongodb,Spring Data Mongodb,我有一个使用spring框架和mongoDB作为数据库的应用程序。我有一个通过电子邮件索引的收藏。这些字段是: { email: <email> profile: { name: <name> age: <age> customMap: { } } } 用例是电子邮件字段是唯一的。因此,当一个请求与现有的电子邮件一起出现时,我需要用新的配置文件更新文档。我可以使用mong

我有一个使用spring框架和mongoDB作为数据库的应用程序。我有一个通过电子邮件索引的收藏。这些字段是:

{
    email: <email>
    profile: {
        name: <name>
        age: <age>
        customMap: {
        }
    }
}
用例是电子邮件字段是唯一的。因此,当一个请求与现有的电子邮件一起出现时,我需要用新的配置文件更新文档。我可以使用
mongoOperations.save
call插入新文档。但在进行更新时,如果我将新地图传递给customProfile,我会得到

java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
作为一个错误。但如果我尝试只更新
age
字段,它将成功更新。我只在使用映射时遇到问题,而我使用的是
HashMap

这是更新的代码:

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);

如果我注释掉
profile.setCustomMap(customMap)然后它成功更新。我遗漏了什么吗?

请确保在您的用户对象中设置电子邮件。否则数据库将不知道更新哪个用户。

似乎这会有帮助。@ArunAK在上面的示例中,他们没有使用spring。我面临的唯一问题是当我引入map.user对象时,它设置了email。查询:Query.addCriteria(Criteria.where(“email”).is(user.getEmail());最糟糕的是它没有显示stacktrace中的空值。我得到的只是“这个参数是必需的;它不能为null”,您可以使用MongoDB管理工具查看您的数据库。我用它
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);