mongodb 查询数组中某一项
1. mongo数据结构如下{"_id": ObjectId("62625d6e418c6626322314e1"),"task_list": [{"task_flow_id": NumberInt("1"),"task_flow_name": "申报过程","status": NumberInt("3"),"longterm": NumberInt("1"),
·
1. mongo数据结构如下
{
"_id": ObjectId("62625d6e418c6626322314e1"),
"task_list": [
{
"task_flow_id": NumberInt("1"),
"task_flow_name": "申报过程",
"status": NumberInt("3"),
"longterm": NumberInt("1"),
"oper_id": "123",
"oper_name": "张三",
"create_time": "2022-04-22 15:46:54",
"update_time": "2022-04-24 10:08:19"
},
{
"task_flow_id": NumberInt("2"),
"task_flow_name": "申报评审过程",
"status": NumberInt("2"),
"longterm": NumberInt("1"),
"oper_id": "123",
"oper_name": "张三",
"create_time": "2022-04-22 15:46:54",
"update_time": "2022-04-24 10:08:38"
},
{
"task_flow_id": NumberInt("3"),
"task_flow_name": "合同过程",
"status": NumberInt("2"),
"start_date": "2022-04-09 00:00:00",
"end_date": "2022-05-10 00:00:00",
"oper_id": "123",
"oper_name": "张三",
"create_time": "2022-04-22 15:46:54",
"update_time": "2022-04-24 10:17:50"
},
{
"task_flow_id": NumberInt("4"),
"task_flow_name": "成员变更过程",
"status": NumberInt("2"),
"create_time": "2022-04-22 15:46:54",
"update_time": "2022-04-24 10:36:34",
"end_date": "",
"longterm": NumberInt("1"),
"oper_id": "123",
"oper_name": "张三",
"start_date": ""
},
{
"task_flow_id": NumberInt("5"),
"task_flow_name": "延期变更过程",
"status": NumberInt("1"),
"create_time": "2022-04-22 15:46:54",
"update_time": "2022-04-22 15:46:54"
},
{
"task_flow_id": NumberInt("6"),
"task_flow_name": "终止变更过程",
"status": NumberInt("1"),
"create_time": "2022-04-22 15:46:54",
"update_time": "2022-04-22 15:46:54"
},
{
"task_flow_id": NumberInt("7"),
"task_flow_name": "验收过程",
"status": NumberInt("1"),
"create_time": "2022-04-22 15:46:54",
"update_time": "2022-04-22 15:46:54"
}
],
"create_time": "2022-04-22 15:46:54",
"update_time": "2022-04-24 10:19:19",
"name": "脑梗研究计划2",
"plan_year": "2022"
}
2. 查询
查询task_list中task_flow_id为2d 数据
db.plan.find({ "_id": ObjectId("62621f42c2d824376d2bb35b"), "task_list.task_flow_id":2}, {"_id": 0, "task_list.$": 1});
3. 更新
更新tasklist中满足条件的状态
db.plan.update({
"_id": ObjectId("62621f42c2d824376d2bb35b"),
"task_list.task_flow_id": 1
}, {
"$set": {"task_list.$.status": 2}
});
4. java代码
- 获取task
public Task getTask(String planId, int flowTaskId) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(planId));
query.addCriteria(Criteria.where("task_list.task_flow_id").is(flowTaskId));
query.fields().exclude("_id");
query.fields().include("task_list.$");
Plan plan = this.mongoTemplate.findOne(query, Plan.class, this.col);
if (Objects.isNull(plan)) { return null; }
List<Task> taskList = plan.getTaskList();
if (Objects.isNull(taskList) || taskList.size() < 1) { return null; }
return taskList.get(0);
}
- 更新task
public void updateTask(String planId, Task task) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(planId));
query.addCriteria(Criteria.where("task_list.task_flow_id").is(task.getTaskFlowId()));
Update update = new Update();
update.set("task_list.$.status", task.getStatus());
update.set("task_list.$.longterm", task.getLongterm());
update.set("task_list.$.start_date", task.getStartDate());
update.set("task_list.$.end_date", task.getEndDate());
update.set("task_list.$.oper_id", task.getOperId());
update.set("task_list.$.oper_name", task.getOperName());
update.set("task_list.$.update_time", task.getUpdateTime());
this.mongoTemplate.updateFirst(query, update, this.col);
}
更多推荐
所有评论(0)