加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何使用Spring Data和MongoDB更新Object?

发布时间:2020-12-15 01:24:27 所属栏目:大数据 来源:网络整理
导读:如何使用Spring Data和MongoDB更新Object? 我只是做一个template.save()? public Person update( String id,String Name ) { logger.debug("Retrieving an existing person"); // Find an entry where pid matches the id Query query = new Query(where("

如何使用Spring Data和MongoDB更新Object?

我只是做一个template.save()?

  public Person update( String id,String Name ) 
    {
        logger.debug("Retrieving an existing person");
        // Find an entry where pid matches the id

        Query query = new Query(where("pid").is(id));
        // Execute the query and find one matching entry
        Person person = mongoTemplate.findOne("mycollection",query,Person.class);

        person.setName(name);
        /**
        * How do I update the database
        */

        return person;
    }
最佳答案
如果您阅读了MongoOperations / MongoTemplate的javadoc,您会看到

save()

执行:

upsert() 

所以是的,你可以只更新你的对象并调用save.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读