Mongodb使用总结
- 基于内存操作,便于与网站交互
- 数据库-集合-文档(存储多种数据类型),我们的操作都是基于单文档进行操作,并且通过冗余字段进行操作
- 嵌入式数组文档减少了对昂贵连接的需求
- DoucmentArray:[子文档(动态)]
操作
都是基于文档操作
@Document("wf_process")@Data@NoArgsConstructor@FieldNameConstantspublic class ProcessDefinition implements SynchronizerSign { /** * 流程ID */ @Id //与数据库_id映射,@Document("wf_process") @Indexed(unique = true)索引3 @JsonSerialize(using = ToStringSerializer.class) 精度丢失 private Long procId;
文档
//增加文档ProcessDefinition resProcess = mongoTemplate.insert(wfProcess);//改Query query = new Query();Update update = new Update();Criteria criteriaOne = Criteria.where(ProcessDefinition.Fields.procId).is(processVo.getProcId());//找到文档update.set(ProcessDefinition.Fields.procName, procName);//修改的属性mongoTemplate.updateMulti(query,update,ProcessDefinition.class);//批量修改//查询ProcessDefinition wfProcess = mongoTemplate.findOne(query, ProcessDefinition.class);//find查询满足条件的文档集合//删除 逻辑删除 update 物理删除remove//query.fields进行字段过滤
文档数组
Query query = new Query(); Update update = new Update(); query.addCriteria(Criteria.where(ProcessDefinition.Fields.procId).is(pub.getProcId()));//找到文档//添加元素到数组 addToSet去重update.addToSet(ProcessDefinition.Fields.flowNodes,pub); mongoTemplate.upsert(query,update,ProcessDefinition.class);//upsert 找不到文档时会自动创建文档//不去重效率高update.push("likes").each(objects);//更新数组元素Criteria.where(ProcessDefinition.Fields.flowNodes). elemMatch(Criteria.where(PublicFlowNode.Fields.nodeId).is(pub.getNodeId())));//elemMatch找到数组元素 flowNodes.nodeID = nodeIDupdate.set("flowNodes.$",pub);//$获取到的元素 query.addCriteria(Criteria.where(ProcessInstanceData.Fields.dataId) .is(dataId) .and("instanceData.flowNodeId") .is(deleteId)); mongoTemplate.updateFirst(query,update,ProcessDefinition.class);//更新数组元素某个属性update.set("flowNodes.$.nextId", nodeId);//获取数组中的元素query.fields().elemMatch(ProcessInstanceData.Fields.instanceData, Criteria.where(InstanceData.Fields.flowNodeId). is(nodeId));//fields()过滤字段 ProcessInstanceData processInstanceData = mongoTemplate.findOne(query, ProcessInstanceData.class);List instanceDatas = processInstanceData.getInstanceData();instanceDatas.get(0);//获取数组中的指定几个元素//封装对象列表查询条件 List commonOperations = new ArrayList(); //1. 指定查询主文档 MatchOperation match = Aggregation.match(Criteria.where(ProcessInstanceData.Fields.dataId). is(dataId)); commonOperations.add(match); //2. 指定投影,返回哪些字段 ProjectionOperation project = Aggregation.project(ProcessInstanceData.Fields.instanceData); commonOperations.add(project); //3. 拆分内嵌文档, 可以查询需要的记录 UnwindOperation unwind = Aggregation.unwind(ProcessInstanceData.Fields.instanceData); commonOperations.add(unwind); //4. 指定查询 指定几个数组元素 MatchOperation match2 = Aggregation.match( Criteria.where("instanceData.flowNodeId").in(nodeIds)); commonOperations.add(match2); //5.创建管道查询对象 Aggregation aggregation = Aggregation.newAggregation(commonOperations); AggregationResults aggregate = mongoTemplate .aggregate(aggregation, ProcessInstanceData.class, JSONObject.class);//删除数组元素instanceData = 删除的对象update.pull(ProcessInstanceData.Fields.instanceData,instanceData);