springboot mongodb crud仅更新更改字段
您好,我有带mongodb的springboot(SpringBootStarter数据mongodb) 我的问题是,如果我只发送一个或只发送我想要更改的字段,那么其他值将设置为null。我在互联网上找到了类似@DynamicUpdate的东西,但没有在mongodb上工作。你能帮我解决这个问题吗。我是一个初学者,我不知道如何帮助,这对我很重要,如果你需要更多的代码或更多的信息,我会写在评论中。我希望我已经充分描述了这个问题。:) 我的POJO:springboot mongodb crud仅更新更改字段,spring,mongodb,spring-boot,upgrade,mongorepository,Spring,Mongodb,Spring Boot,Upgrade,Mongorepository,您好,我有带mongodb的springboot(SpringBootStarter数据mongodb) 我的问题是,如果我只发送一个或只发送我想要更改的字段,那么其他值将设置为null。我在互联网上找到了类似@DynamicUpdate的东西,但没有在mongodb上工作。你能帮我解决这个问题吗。我是一个初学者,我不知道如何帮助,这对我很重要,如果你需要更多的代码或更多的信息,我会写在评论中。我希望我已经充分描述了这个问题。:) 我的POJO: @Data @Getter @Setter @N
@Data
@Getter
@Setter
@NoArgsConstructor
public class Person {
@Id
private String id;
private String firstName;
private String lastName;
private boolean enabled;
private String note;
回购
样本:
更新前获取呼叫:
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Rambo",
"lastName": "Norris",
"enabled": true,
"note": "hello this is my first note from you",
}
{
"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"
}
更新后获取呼叫:
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Chuck",
"lastName": null,
"enabled": false,
"note": null,
}
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Chuck",
"lastName": "Norris",
"enabled": true,
"note": "hello this is my first note from you",
}
我想要什么
更新前获取呼叫:
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Rambo",
"lastName": "Norris",
"enabled": true,
"note": "hello this is my first note from you",
}
{
"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"
}
更新后获取呼叫:
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Chuck",
"lastName": null,
"enabled": false,
"note": null,
}
{
"id": "5fc940dc6d368377561dbb02",
"firstName": "Chuck",
"lastName": "Norris",
"enabled": true,
"note": "hello this is my first note from you",
}
您正在插入一个新集合,而不是更新。首先,需要从mongodb获取旧值,然后需要更新集合,然后保存到DB 在
@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 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
值(lastName
,note
等)除了FirstName
。您正在将所有值从person
设置为personFromDB
对象,因此除了FirstName
之外,您正在保存空值。请检查json
输入请求或在此处发布json
响应。