I. know mongodb
MongoDB is a product between relational database and non relational database. It has the most abundant functions and is the most like relational database. It supports a very loose data structure, which is similar to json's bson format, so it can store more complex data types. Mongo's biggest feature is that it supports a very powerful query language, and its syntax is a bit similar to the object-oriented query language. It can almost achieve most of the functions similar to the single table query of relational database, and it also supports data indexing.
II. Spring boot project integration mongodb
1. Add mongodb dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
2. Configure mongodb connection
spring: data: mongodb: #uri: mongodb://localhost:27017/data_exploration uri: mongodb://root:dhcc-mongodb@192.168.100.87:27017/data_exploration?authSource=admin&authMechanism=SCRAM-SHA-1
Resolution: the above URIs represent local configuration and remote connection respectively
3. Operation database
(1) preservation
@Repository public class ExplorationJobDao { @Autowired MongoTemplate mongoTemplate; public void save(ExplorationJob explorationJob) { mongoTemplate.save(explorationJob); } }
(2) modify a piece of data according to ID (the principle of which first conforms to the data of ID, and then delete the first piece of query result)
public void updateExecutionStatusById(int executionStatus, String jobId) { Query query = new Query(Criteria.where("jobId").is(jobId)); Update update = new Update().set("executionStatus", executionStatus); mongoTemplate.updateFirst(query, update, ExplorationJob.class); }
(3) modify multiple data according to (query all data conforming to ID, and then modify all data)
public void update(BusinessExploration businessExploration) { Query query = new Query(Criteria.where("_id").is(businessExploration.getId())); Update update = new Update().set("sourceUnit", businessExploration.getSourceUnit()) .set("appSystem", businessExploration.getAppSystem()) .set("businessImplication", businessExploration.getBusinessImplication()) .set("safetyRequire", businessExploration.getSafetyRequire()); mongoTemplate.updateMulti(query, update, TableExploration.class); }
(4) delete (delete according to ID)
public void delExplorationJobById(String jobId) { Query query=new Query(Criteria.where("jobId").is(jobId)); mongoTemplate.remove(query,ExplorationJob.class); }
(5) query by criteria (query by ID)
public ExplorationJob getExplorationJobByJobId(String jobId) { Query query = new Query(Criteria.where("jobId").is(jobId)); ExplorationJob explorationJob = mongoTemplate.findOne(query, ExplorationJob.class); return explorationJob; }
(6) query all
mongoTemplate.findAll(TableExploration.class);
(7) multi condition dynamic query
public List<ExplorationJob> getExplorationByCondition(ExplorationJob explorationJob) { Query query = new Query(); if (explorationJob.getJobName() != null) { Pattern pattern = Pattern.compile("^.*" + explorationJob.getJobName() + ".*$", Pattern.CASE_INSENSITIVE); query.addCriteria(Criteria.where("jobName").regex(pattern)); } if (explorationJob.getDsType() != null) { query.addCriteria(Criteria.where("dsType").is(explorationJob.getDsType())); } if (explorationJob.getExecutionStatus() != null) { query.addCriteria(Criteria.where("executionStatus").lte(explorationJob.getExecutionStatus())); } List<ExplorationJob> explorationJobs=mongoTemplate.find(query, ExplorationJob.class); return explorationJobs; }
(8) query maximum
public Date getMaxExplorationDate(String tableName) { FindIterable<Document> iterable = mongoTemplate.getCollection("tableExploration") .find(new BasicDBObject("tableName", tableName)).sort(new BasicDBObject("explorationDate", -1)).skip(0) .limit(1); Document doc =null; if (getDocuments(iterable).size()>0) { doc=getDocuments(iterable).get(0); Date date = doc.getDate("explorationDate"); return date; }else { return null; } } /** * Tool method * * @param iterable * @return */ public static List<Document> getDocuments(FindIterable<Document> iterable) { List<Document> results = new ArrayList<Document>(); if (null != iterable) { MongoCursor<Document> cursor = iterable.iterator(); Document doc = null; while (cursor.hasNext()) { doc = cursor.next(); results.add(doc); } } return results; }
(9) group query (sorting is still used here)
public List<TableExploration> getAllTableExplorationGroupByTableName(String jobId){ Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(Criteria.where("jobId").is(jobId)), Aggregation.sort(new Sort(Direction.DESC,"explorationDate")), Aggregation.group("tableName") .first("_id").as("tableName") .first("databaseType").as("databaseType") .first("databaseName").as("databaseName") .first("networkSituation").as("networkSituation") .first("userName").as("userName") .first("password").as("password") .first("url").as("url") .first("dataStorage").as("dataStorage") .first("dataIncrement").as("dataIncrement") .first("explorationDate").as("explorationDate") //.push("columnExplorations").as("columnExplorations") .first("jobId").as("jobId") ); AggregationResults<TableExploration> aggregationResults= mongoTemplate.aggregate(aggregation, "tableExploration", TableExploration.class); List<TableExploration> tableExplorations=aggregationResults.getMappedResults(); return tableExplorations;
[common operation summary completed]