MongoTemplate更新实体类的最优方法

ITWYY

关注

阅读 81

2023-03-04


直接上代码

@PostMapping("/update")
public Object update(@RequestBody URLDTO urldto) throws Exception{
urldto.setName("update");
Query query = new Query();
query.addCriteria(Criteria.where("uid").is(urldto.getUid()));
BasicUpdate update = new BasicUpdate(
JSON.toJSONString(urldto)
);
return mongoTemplate.updateFirst(query, update, GlobalCollectionName.Collection_URL);
}


@PostMapping("/updateSelect")
public Object updateSelect(@RequestBody URLDTO urldto) throws Exception{
urldto.setName("update");
Query query = new Query();
query.addCriteria(Criteria.where("uid").is(urldto.getUid()));
String data=JSON.toJSONString(urldto);
BasicUpdate update = new BasicUpdate(
"{"+"'$set':"+data+"}"
);
return mongoTemplate.updateFirst(query, update, GlobalCollectionName.Collection_URL);
}


精彩评论(0)

0 0 举报