import org.springframework.data.mongodb.core.MongoTemplate;
@Autowired(required = false)
private MongoTemplate mongoTemplate;
public List<DbaIncident> getDbaIncidentListByCriteria(Map<String, Object> params,Integer start, Integer limit) {
List<DbaIncident> DbaIncidentList = new ArrayList<>();
BasicDBObject query = new BasicDBObject();
BasicDBList dbs = MongoDBUtils.matchClosureTime("alarm_time", start_time, end_time);
//事件段约束
query.append("$and", dbs);
//模糊搜索
BasicDBList dbList = new BasicDBList();
if(tableName.equals("t_data_alarm_url")){
dbList.add(new BasicDBObject("biz_name", new BasicDBObject("$regex", keyWord).append("$options", "$i")));
dbList.add(new BasicDBObject("app_name", new BasicDBObject("$regex", keyWord).append("$options", "$i")));
dbList.add(new BasicDBObject("server_ip", new BasicDBObject("$regex", keyWord).append("$options", "$i")));
}
BasicDBObject or = new BasicDBObject("$or", dbList);
dbs.add(or);
if (StringUtil.isNotEmpty(client_mac)) {
//模糊匹配查询
Pattern pattern = Pattern.compile("^.*"+client_mac+".*$", Pattern.CASE_INSENSITIVE);
query.append("client_mac", pattern);
}
List<String> apiUuidList = new ArrayList();
query.append("app_uuid",new BasicDBObject("$in", apiUuidList));
MongoCollection<Document> dBCollection = mongoTemplate.getCollection(tableName);
FindIterable<Document> urlDocument= dBCollection.find(query).sort(new BasicDBObject("alarm_time", -1)).skip(start).limit(limit);
MongoCursor<Document> iterator = urlDocument.iterator();
while (iterator.hasNext()) {
Document next = iterator.next();
try {
DbaIncident dbaIncident = JsonUtil.jsonToObject(next.toJson(), DbaIncident.class);
DbaIncidentList.add(dbaIncident);
} catch (Exception e) {
e.printStackTrace();
}
}
}
当更新的字段不存在时,自动创建该字段,默认值为当前更新的值
public Boolean updateSafeBulltinStatus(Map<String, Object> params) {
try {
String tableName = "";
String sourceType = String.valueOf(params.get("sourceType"));
if ("database".equals(sourceType)) {
Integer alarmTime = (Integer) params.get("alarmTime");
tableName = "t_data_alarm_dbsql_" + DateUtil.intToStringDate(alarmTime, DateUtil.DEFAULT_DATE_FORMAT);
}else if ("bussiness".equals(sourceType)) {
tableName = "t_data_alarm_url";
}
BasicDBObject where = new BasicDBObject("alarm_id", String.valueOf(params.get("alarmId")));
BasicDBObject set = new BasicDBObject("$set", new BasicDBObject("safeStatus", (Integer) params.get("safeStatus")));
mongoTemplate.getCollection(tableName).updateOne(where, set);
}catch (Exception e) {
e.printStackTrace();
return Boolean.FALSE;
}
return Boolean.TRUE;
}
更新一调数据的说个字段值
BasicDBObject where = new BasicDBObject("alarm_id", incidentUuid);
BasicDBObject condition = new BasicDBObject("status",IncidentStatusEnum.CLOSED.getValue());
condition.append("close_time",DateUtil.getCurrentTime());
condition.append("close_cause",closeIncidentDto.getCloseCause());
BasicDBObject set = new BasicDBObject("$set", condition );
String tableName = "";
mongoTemplate.getCollection(tableName).updateOne(where, set);
在 MongoDB 中,我们如果我们需要更新多条记录,既可以使用 update 函数,还可以使用 updateMany 函数,官方推荐使用 updateMany 函数。
mongoTemplate.getCollection(tableName).updateMany({"url" : "www.haicoder.net"}, {$set:{"url":"haicoder.net"}})
mongoTemplate.getCollection(tableName).insertMany([{"name":"haicoder", "url":"www.haicoder.net"},
{"name":"HaiCoder", "url":"www.haicoder.net"},
{"name":"golang", "url":"https://haicoder.net/golang/golang-tutorial.html"}])