添加:
mongoTemplate.insert(对象);
更新(查询出来符合条件再更新):
Query query = new Query(Criteria.where(KEY_ORG_ID).is(taskPO.getOrgId()). and(KEY_PK_ID).is(taskPO.getId()));
Update update = new Update();
update.set(KEY_TITLE, taskPO.getTitle());
mongoTemplate.upsert(query, update, 对象.class);
更新对象得数组(某一条数据)
Query query = new Query(Criteria.where("id").is(taskId));
Update update = new Update();
update.set("数组对象.$[数组对象].字段", content);
update.set("数组对象.$[数组对象].字段", true);
if (!ToolsUtils.isEmpty(date)) {
update.set("数组对象.$[数组对象].字段", date);
}
update.filterArray(Criteria.where("数组对象.字段").is(wxUserId));
mongoTemplate.updateFirst(query, update, CalendarTaskPO.class);
查询:
简单查询:
Query query = new Query(Criteria.where(KEY_ORG_ID).is(orgId).
and(KEY_PK_ID).is(id).and(KEY_PK_IS_DELETE).is(Boolean.FALSE));
return mongoTemplate.findOne(query, 对象.class);
子查询匹配(数组):
Query query = new Query(Criteria.where(KEY_STATUS).is(TaskStatusEnum.ONGOING.getStatus()).and(KEY_FEEBDACKLIST).elemMatch(Criteria.where(KEY_RESPONSIBLE_PERSON).is(wxUserId).and(KEY_IS_FEEDBACK).is(Boolean.FALSE)));
return mongoTemplate.count(query, 对象.class);
复杂查询:
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(criteria),
Aggregation.unwind(CalendarTaskDict.KEY_FEEBDACKLIST),
Aggregation.match(Criteria.where("feedbackList.responsiblePerson").is(wxUserId)),
Aggregation.sort(Sort.by(Sort.Order.asc("feedbackList.isFeedBack"),Sort.Order.desc(KEY_CREATE_TIME))),
Aggregation.skip(currentPage > 1 ? (currentPage - 1) * pageSize : 0),
Aggregation.limit(pageSize),
Aggregation.project(KEY_PK_ID, KEY_CONTENT, KEY_TITLE, KEY_CALENDAR_ID, KEY_STATUS,KEY_ONGOINGTYPE, KEY_CREATE_TIME,"feedbackList.isFeedBack")
);
AggregationResults<CalendarTaskPageVO> calendarTaskAggregationResults = mongoTemplate.aggregate(aggregation, DOC_CALENDER_TASK, 对象(可以不对应PO).class);
List<对象> mappedResults = calendarTaskAggregationResults.getMappedResults();
return mappedResults;
删除:
Criteria idCriteria = Criteria.where(KEY_ORG_ID).is(orgId).and(KEY_CALENDAR_ID).is(calendarId);
return mongoTemplate.remove(new Query(idCriteria), CalendarTaskPO.class);
mogodbTemplate简单使用
最新推荐文章于 2024-06-06 22:43:05 发布