【问题标题】:springboot mongodb crud update only changed fieldsspringboot mongodb crud update 只改变了字段
【发布时间】:2021-03-16 08:48:58
【问题描述】:

您好,我有带有 mongodb 的 springboot (spring-boot-starter-data-mongodb)

我的问题是如果我只发送一个或只发送我想要更改的字段,那么其他值设置为空。我在互联网上发现了一些东西,比如@DynamicUpdate,但不能在 mongodb 上工作,你能帮我解决这个问题吗?我是初学者,我不知道如何提供帮助,这对我来说很重要,如果您需要更多代码或更多信息,我会写在评论中。我希望我已经充分描述了这个问题。 :)

我的 POJO:

@Data
@Getter
@Setter
@NoArgsConstructor
public class Person {

    @Id
    private String id;
    private String firstName;
    private String lastName;
    private boolean enabled;
    private String note;

回购

@Repository
public interface PersonRepository  extends MongoRepository <Person, String> {
}

我有这个电话

@PutMapping("/{id}")
@ResponseBody
public void UpdatePerson (@PathVariable String id , @RequestBody Person person) {
    personRepository.save(person);
}

@GetMapping(path = "/{id}")
public Person getPersonByid(@PathVariable String id ){
    return personRepository.findById(id).orElseThrow(PersonNotFound::new);
}

样本:

更新前打电话:

{
    "id": "5fc940dc6d368377561dbb02",
    "firstName": "Rambo",
    "lastName": "Norris",
    "enabled": true,
    "note": "hello this is my first note from you",
}

拨打电话:

{
    "id": "5fc940dc6d368377561dbb02",
    "firstName": "Chuck"
}

更新后接电话:

{
    "id": "5fc940dc6d368377561dbb02",
    "firstName": "Chuck",
    "lastName": null,
    "enabled": false,
    "note": null,
}

我想要什么

更新前打电话:

{
    "id": "5fc940dc6d368377561dbb02",
    "firstName": "Rambo",
    "lastName": "Norris",
    "enabled": true,
    "note": "hello this is my first note from you",
}

拨打电话:

{
    "id": "5fc940dc6d368377561dbb02",
    "firstName": "Chuck"
}

更新后接电话:

{
    "id": "5fc940dc6d368377561dbb02",
    "firstName": "Chuck",
    "lastName": "Norris",
    "enabled": true,
    "note": "hello this is my first note from you",
}

【问题讨论】:

    标签: spring mongodb spring-boot upgrade mongorepository


    【解决方案1】:

    您正在插入一个新集合,而不是更新。首先,您需要从 mongodb 获取旧值,然后您需要更新集合,然后保存到数据库

    @putmapping中使用下面的代码。

    @PutMapping("/{id}")
    @ResponseBody
    public void UpdatePerson (@PathVariable String id , @RequestBody Person person) {
        Person personFromDB = personRepository.findById(person.getId());
        personFromDB.setFirstName(person.getFirstName());
        personRepository.save(personFromDB);
    }
    

    【讨论】:

    • 当我这样做时``` @PutMapping("/{id}") @ResponseBody public void UpdatePerson (@PathVariable String id , @RequestBody Person person) { Person personFromDB = personRepository.findById(person .getId()); personFromDB.setFirstName(person.getFirstName()); personFromDB.setNote(person.getNote()); ..... personFromDB.setX(person.getX()); personRepository.save(personFromDB); } ``` 并且在请求中是 ``` { "id": "5fb5755603d57a5a3c9f6a63", "firstName": "Chuck" } ``` 然后其他类似(注意,lastName .....,x)将它们设置为 null ...
    • 我可以理解person 对象收到了null 值(lastNamenote 等)而不是FirstName。您将所有值从person 设置为personFromDB 对象,因此除了FirstName 之外,您还保存了空值。检查您的json 输入请求或在此处发布您的json 回复。
    【解决方案2】:

    尝试像这样更新

      @PutMapping("/{id}")  
      public ResponseEntity<Person> UpdatePerson (@PathVariable String id , @RequestBody 
      Person person) {
    
        Optional<Person> personData = personRepository.findById(id);
            if (personData.isPresent()) {
              Person _tutorial = personData.get();
              if(!StringUtils.isEmpty(person.getFirstName())) {
             _tutorial.setFirstName(person.getFirstName());
              }
              if(!StringUtils.isEmpty(person.getLastName())) {
             _tutorial.setLastName(person.getLastName());
              }
              if(!StringUtils.isEmpty(person.getNote())) {
             _tutorial.setNote(person.getNote());
              }
              if(!StringUtils.isEmpty(tutorial.isEnabled())) {
             _tutorial.setEnabled(tutorial.isEnabled());
             }
              return new ResponseEntity<>(repo.save(_tutorial), HttpStatus.OK);
            } else {
              return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      相关资源
      最近更新 更多