springboot mongodb crud 更新仅更改字段
springboot mongodb crud update only changed fields
你好,我有spring引导 mongodb (spring-boot-starter-data-mongodb)
我的问题是,如果我只发送一个或只发送我想更改的字段,那么其他值将设置为空。我在 Internet 上找到了类似 @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",
}
您正在插入一个新集合而不是更新。首先,您需要从 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}")
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);
}
}
你好,我有spring引导 mongodb (spring-boot-starter-data-mongodb)
我的问题是,如果我只发送一个或只发送我想更改的字段,那么其他值将设置为空。我在 Internet 上找到了类似 @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",
}
您正在插入一个新集合而不是更新。首先,您需要从 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}")
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);
}
}