mongoTemplate通过upsert实现数据存在就更新,不存在就插入数据

// 查询条件,如果数据存在更新
Query query = new Query();
query.addCriteria(Criteria.where("statisticsDatetime").is(e.getStatisticsDatetime()));
query.addCriteria(Criteria.where("storeId").is(e.getStoreId()));

// 更新的字段
Update update = new Update();
update.set("expenseStatisticsList", e.getExpenseStatisticsList());
update.set("parentStoreId", e.getParentStoreId());
update.set("storeType", e.getStoreType());
mongoTemplate.upsert(query, update, ContractItemExpenseStatistics.class, MongodbTableEnum.expense_statistics.getValue());

 

你可能感兴趣的:(mongodb)