Linux安装mongodbl参考链接
注意配置文件:
#mongodb所绑定的ip地址
bind_ip = 0.0.0.0
1. spring data mongodb 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2. mongodb连接配置
package org.jeecg.modules.daxt.config;
import com.mongodb.client.MongoClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
import org.springframework.data.mongodb.core.MongoTemplate;
@Configuration
public class MongoConfig {
@Bean
public MongoClientFactoryBean mongoClientFactoryBean() {
MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
factoryBean.setHost("192.168.x.x");//ip
factoryBean.setPort(27017);//端口
return factoryBean;
}
@Bean
public MongoTemplate mongoTemplate( MongoClient mongoClient) {
return new MongoTemplate(mongoClient, "daxt");//数据库
}
}
3. mongodb数据库存储数据格式
db.getCollection("monitorData").insert([ {
_id: ObjectId("62b009024be76750ffbe5893"),
jcjcsjjrjlId: "19079803c44844b2abea39873530f2f6",
sbjcpzid: "1536949999094943746",
date: "2000-0-0",
time: "0:00:00",
startupTime: "31152",
data: {
"节点通信状态": "28",
"停车": "无效状态",
"空调高压指令": "NULL",
"停车警示灯": "掉电状态(指示灯灭)",
"辅助启动": "无效",
"预润泵指示灯": "掉电状态(指示灯灭)",
"柴油泵指示灯": "掉电状态(指示灯灭)",
"战斗状态": "无效状态",
"左转向指示灯": "掉电状态(指示灯灭)",
"高压超级电容不充电输出": "输出关闭",
"启动加温指示灯": "掉电状态(指示灯灭)",
"示廓指示灯": "掉电状态(指示灯灭)",
"报警等级": "3",
"空调高压指示灯": "掉电状态(指示灯灭)",
"声音报警指令": "无报警",
"伪装灯指示灯": "掉电状态(指示灯灭)",
"吹洗装置": "掉电状态(指示灯灭)",
"视频电源指示灯": "掉电状态(指示灯灭)",
"右转向指示灯": "掉电状态(指示灯灭)",
"战斗状态指示灯": "掉电状态(指示灯灭)",
"炮塔高压指令": "NULL",
"前大灯指示灯": "掉电状态(指示灯灭)"
},
_class: "org.jeecg.modules.daxt.entity.MonitorDataFrame"
} ]);
4. mongodb数据库聚合查询
db.monitorData.aggregate(
[{
$group: {
_id: {
jcjcsjjrjlId: "$jcjcsjjrjlId",
sbjcpzid: "$sbjcpzid"
},
mindatetime: {
$min: {
$concat: ["$date"," ","$time" ]
}
},
maxdatetime: {
$max: {
$concat: ["$date"," ","$time" ]
}
}
}
}]
)
5. mongodb java代码聚合查询
//MongoDB中存储 某次 接入记录jcjcsjjrjlIds 某 命令帧sbjcpzid 监测点 最早-最晚时间
private List<Map> getJcdList(List<String> jcjcsjjrjlIds,List<String> sbjcpzIds) {
Criteria criteria1 = new Criteria("jcjcsjjrjlId").in(jcjcsjjrjlIds);
AggregationOperation match1 = Aggregation.match(criteria1);
Criteria criteria2 = new Criteria("sbjcpzid").in(sbjcpzIds);
AggregationOperation match2 = Aggregation.match(criteria2);
//聚合表达式 年月日 时分秒
AggregationExpression concatString = new AggregationExpression() {
@Override
public Document toDocument(AggregationOperationContext context) {
return new Document("$concat", new Object[]{"$date"," ","$time"});
}
};
//聚合
AggregationOperation group = Aggregation.group("jcjcsjjrjlId","sbjcpzid")
.min(concatString).as("mindatetime")
.max(concatString).as("maxdatetime");
Aggregation aggregationObj = newAggregation(match1,match2, group);
List<Map> mappedResults = template.aggregate(aggregationObj, "monitorData", Map.class).getMappedResults();//mongodb存储监测点记录
return mappedResults;
}