使用 spring 数据 mongo 插入 Mongo 文档 [英] Upsert Mongo Document using spring data mongo
问题描述
我有一个班
@Document
public class MyDocument {
@Id
private String id;
private String title;
private String description;
private String tagLine;
@CreatedDate
private Date createdDate;
@LastModifiedDate
private Date updatedDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTagLine() {
return tagLine;
}
public void setTagLine(String tagLine) {
this.tagLine = tagLine;
}
}
我用 @EnableMongoAuditing
我已经创建了实现 mongorepository 的接口
i have created interface which implements mongorepository
公共接口 MyDocumentRepository 扩展了 MongoRepository
当我使用 GET
,POST
,PATCH
方法创建 RestController 时在 POST
我发送{'title':'first'}
when i have created RestController with GET
,POST
,PATCH
methods
in POST
I'm sending
{'title':'first'}
控制器类的POST方法是
Controller Class POST method is
@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity<?> saveMyDocument(@RequestBody MyDocument myDocument) {
MyDocument doc = myDocumentRepo.save(myDocument);
return new ResponseEntity<MyDocument>(doc, HttpStatus.CREATED);
}
它将数据保存在 mongo 中.
Its saving the data in mongo.
{
"_id" : ObjectId("56b3451f0364b03f3098f101"),
"_class" : "com.wiziq.service.course.model.MyDocument",
"title" : "test"
}
和 PATCH 请求就像
and PATCH request is like
@RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
public ResponseEntity<MyDocument> updateCourse(@PathVariable(value = "id") String id,
@RequestBody MyDocument myDocument) {
myDocument.setId(id);
MyDocument doc = courseService.save(myDocument);
return ResponseEntity.ok(course);
}
在使用数据进行 PATCH 请求时 {"description":"This is test"}
它更新文档 BUT 它从文档中删除标题字段和 createdDate,它的更新是可以的.但我想做一个 upsert,我可以使用 mongoTemplate,但是我必须设置我想要设置的每个属性.
when in make PATCH request with data {"description":"This is test"}
it update the docuent BUT it removes title field and createdDate form the document, its doing update which is ok. But i wanted to do an upsert, i can do its using mongoTemplate,
but there i have to set each property which i want to set.
如果我收到 PATCH 请求,是否有任何通用方法可以更新非空属性.. 请求中的属性
Is there any generic way to that if i get a PATCH request i can update only not null properties.. properties which are coming in request
spring-data-rest 似乎使用@RepositoryRestResource 来实现.我怎样才能达到同样的目标.
spring-data-rest seems to do it using @RepositoryRestResource. How can i achieve the same.
我不想这样编码Update update = new Update().set("title", myDocument.getTitle()).set("description", myDocument.getdescription());
推荐答案
不幸的是它在 MongoDB 中的行为,您可以使用 shell 验证相同.所以要更新创建一个更新对象并使用
Unfortunately its the behavior in MongoDB, you can verify the same using shell. So to update create an Update Object and using
Query query = new Query(Criteria.where("id").is(ID));
这里的 ID 是您要更新的文档.根据您的要求,使用 findAndModify 更新文档之后设置 upsert.
Here ID is the document which you want to update.Based on your requirement set upsert after that using findAndModify update document.
mongoTemplate.findAndModify(query, update,
new FindAndModifyOptions().returnNew(true).upsert(false),
someclass.class);
这篇关于使用 spring 数据 mongo 插入 Mongo 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!