使用spring数据Mongo上传Mongo文档
我有一节课使用spring数据Mongo上传Mongo文档,spring,spring-boot,spring-data,spring-data-mongodb,spring-data-rest,Spring,Spring Boot,Spring Data,Spring Data Mongodb,Spring Data Rest,我有一节课 @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;
@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的接口
公共接口MyDocumentRepository扩展了MongoRepository{
}
当我使用GET
,POST
,PATCH
方法创建RestController时
在POST
中,我正在发送
{'title':'first'}
控制器类POST方法是
@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity<?> saveMyDocument(@RequestBody MyDocument myDocument) {
MyDocument doc = myDocumentRepo.save(myDocument);
return new ResponseEntity<MyDocument>(doc, HttpStatus.CREATED);
}
而补丁请求就像
@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);
}
@RequestMapping(value=“/{id}”,method=RequestMethod.PATCH)
public ResponseEntity updateCourse(@PathVariable(value=“id”)字符串id,
@请求主体(我的文档我的文档){
myDocument.setId(id);
MyDocument doc=courseService.save(MyDocument);
返回应答。ok(当然);
}
在使用数据发出修补程序请求时{“description”:“这是测试”}
它更新文档,但是它删除了文档中的标题字段和createdDate,这样做是可以的。但我想做一个升级,我可以用mongoTemplate来做,
但是在那里我必须设置我想要设置的每个属性
如果我得到一个补丁请求,我可以只更新NOTNULL属性,有没有通用的方法。。正在请求的属性
SpringDataREST似乎使用@RepositoryRestResource来实现这一点。我怎样才能达到同样的效果
我不想这样编码
Update Update=new Update().set(“title”,myDocument.getTitle()).set(“description”,myDocument.getdescription())代码>不幸的是,这是MongoDB中的行为,您可以使用shell验证相同的行为。
因此,要更新,请创建一个更新对象并使用
Query query = new Query(Criteria.where("id").is(ID));
此处ID是您要更新的文档。根据您的需求集,使用findAndModify更新文档在更新后插入
mongoTemplate.findAndModify(query, update,
new FindAndModifyOptions().returnNew(true).upsert(false),
someclass.class);
如果您有一个像MyModel.class这样的模型,并且您需要一种平滑的方法来从中创建更新对象,那么没有真正明确的方法可以做到这一点,但是您可以使用在Spring Data Mongo auto configuration中创建的MongoConverter bean,然后只使用MongoCollection的replaceOne方法
@Autowired
private MongoTemplate template;
@Autowired
private MongoConverter mongoConverter;
...
@Override
public void upsertMyModel(MyModel model) {
Document documentToUpsert = new Document();
mongoConverter.write(model, documentToUpsert);
template.getCollection(collectionName).replaceOne(
Filters.eq("_id", model.getId()),
documentToUpsert,
new ReplaceOptions().upsert(true));
}
你的courseService.save(myDocument)功能在做什么?运气好吗?我一直在寻找解决同样问题的办法。
@Autowired
private MongoTemplate template;
@Autowired
private MongoConverter mongoConverter;
...
@Override
public void upsertMyModel(MyModel model) {
Document documentToUpsert = new Document();
mongoConverter.write(model, documentToUpsert);
template.getCollection(collectionName).replaceOne(
Filters.eq("_id", model.getId()),
documentToUpsert,
new ReplaceOptions().upsert(true));
}