mongoTemplate的方法和(hé)update()
發表時間:2019-9-24
發布人:葵宇科技
浏覽次數:110
1.引言
mongoTemplate提供了許多的方法,許多的類似的方法,剛開始不知道使用哪一種最好,現在,我把他們整理如(rú)下(xià),方便大家選擇使用。
2.常用方法
/**
* 每個(gè)操作帶上collectionName, 效率提高許多,并且,防止子(zǐ)類錯誤操作
*
* @param formDataApproval 對象
* @author leimin
*/
public void mongoTemplateSample(FormDataApproval formDataApproval) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(formDataApproval.getId()).and("tenant_id").is(formDataApproval.getTenantId()));
Update update = new Update();
update.set("process_id", formDataApproval.getProcessId());
List<FormDataApproval> list = new ArrayList<>();
list.add(formDataApproval);
/**
* 數據庫有,就新增, 沒有,就修改
*/
mongoTemplate.upsert(query, update, FormData.class, "formData");
/**
* 修改方法
*/
// 無實際意義,不推薦使用
mongoTemplate.update(FormDataApproval.class);
mongoTemplate.updateFirst(query, update, FormDataApproval.class, "formDataApproval");
mongoTemplate.updateMulti(query, update, FormDataApproval.class, "formDataApproval");
mongoTemplate.findAndModify(query, update, FormDataApproval.class, "formDataApproval");
/**
* 查詢方法
*/
mongoTemplate.find(query, FormDataApproval.class, "formDataApproval");
mongoTemplate.findOne(query, FormDataApproval.class, "formDataApproval");
mongoTemplate.findAll(FormDataApproval.class, "formDataApproval");
mongoTemplate.findById(formDataApproval.getId(), FormDataApproval.class, "formDataApproval");
/**
* 删除方法
*/
mongoTemplate.remove(query, FormDataApproval.class, "formDataApproval");
mongoTemplate.findAndRemove(query, FormDataApproval.class, "formDataApproval");
mongoTemplate.findAllAndRemove(query, FormDataApproval.class, "formDataApproval");
/**
* 新增方法
*/
mongoTemplate.save(formDataApproval, "formDataApproval"); // 同upsert(),沒有新增,有修改
mongoTemplate.insert(formDataApproval, "formDataApproval");
mongoTemplate.insertAll(list);
}
3.批量修改
/**
* 批量修改 formData
*
* @param formDataList formDatas
* @author leimin
*/
@Override
public void updateFormDatas(List<FormData> formDataList) {
if (CollectionUtils.isEmpty(formDataList)) {
return;
}
BulkOperations operations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, Const.COLLECTION_FORM_DATA);
List<Pair<Query, Update>> list = new ArrayList<>();
for (FormData formData : formDataList) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(formData.getId()).and("tenant_id").is(formData.getTenantId()));
Update update = new Update();
update = mongoUtil.getUpdateFormDataAll(formData, update);
Pair<Query, Update> pair = Pair.of(query, update);
list.add(pair);
}
operations.updateMulti(list);
operations.execute();
}
4.其他方法
/**
* 類似于mongo原生sql的操作
* 不推薦使用
*
* @author leimin
*/
public void mongoTempalteAsOriginal() {
// 查詢該集合中(zhōng)的所有文(wén)檔
MongoCollection<Document> formDataApprovalDocument = mongoTemplate.getCollection("formDataApproval");
// 新增
formDataApprovalDocument.insertOne();
formDataApprovalDocument.insertMany();
// 删除
formDataApprovalDocument.deleteOne();
formDataApprovalDocument.deleteMany();
formDataApprovalDocument.findOneAndDelete();
// 修改
formDataApprovalDocument.updateOne();
formDataApprovalDocument.updateMany();
formDataApprovalDocument.findOneAndUpdate();
formDataApprovalDocument.replaceOne();
formDataApprovalDocument.findOneAndReplace();
// 查詢
formDataApprovalDocument.find();
// 獲取集合name
mongoTemplate.getCollectionName(FormDataApproval.class);
}