我想知道有没有办法直接在后端更新mongodb文件并将更新的数据发送到前端,就像我们用来更新MySQL转储文件并将数据发送到前端一样。
您可以使用qongxswpoi方法使用mongoTemplate更新记录并将其返回spring-data-mongodb,有关详细信息,请查看官方文档findAndUpdate
。有关快速解决方案,请查看以下代码:
10.5.6. Finding and Upserting Documents in a Collection
我已经在我的github repo中提交了这些代码,您可以查看它:// find and update and then return
Query query = new Query();
query.addCriteria(Criteria.where("firstName").is("First Name"));
Update update = new Update();
update.set("lastName", "modified last name");
FindAndModifyOptions options = new FindAndModifyOptions();
options.upsert(true);
options.returnNew(true);
try {
Customer modifiedCustomer = mongoOperation
.findAndModify(query, update, options, Customer.class);
// Modified data
System.out.println("Modified Custom data\n");
System.out.println(modifiedCustomer);
// Return from here;
} catch (Exception e) {
System.out.println(e.getMessage());
throw e;
}