springboot mongodb crud update only changed fields

3.1k Views Asked by At

Hello i have springboot with mongodb (spring-boot-starter-data-mongodb)

My problem is if I send only one or only the fields I want to change so the other values are set to null. I found something on the internet like @DynamicUpdate but not working on mongodb can you help me with this problem. I'm a beginner, I don't know how to help and it's quite important for me, if you need more code or more information, I'll write in the comment. I hope I have described the problem sufficiently. :)

MY POJO:

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

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

Repo

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

i have this call

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

sample:

get call before update :

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

put call :

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

get call after update :

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

what I would like

get call before update :

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

put call :

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

get call after update :

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

There are 2 best solutions below

2
On BEST ANSWER

You are inserting a new collection instead of updating. First, you need to get the old value from mongodb, then you need to update the collection, then save to DB.

Use the below code in @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);
}
0
On

Try updating like this

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