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

前端之家收集整理的这篇文章主要介绍了如何使用Spring Data和MongoDB更新Object?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

  1. @H_403_8@ public Person update( String id,String Name )
  2. {
  3. logger.debug("Retrieving an existing person");
  4. // Find an entry where pid matches the id
  5.  
  6. Query query = new Query(where("pid").is(id));
  7. // Execute the query and find one matching entry
  8. Person person = mongoTemplate.findOne("mycollection",query,Person.class);
  9.  
  10. person.setName(name);
  11. /**
  12. * How do I update the database
  13. */
  14.  
  15. return person;
  16. }
最佳答案
如果您阅读了MongoOperations / MongoTemplate的javadoc,您会看到

  1. @H_403_8@save()

执行:

  1. @H_403_8@upsert()

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

猜你在找的Spring相关文章