時(shí)間:2024-03-09 11:51作者:下載吧人氣:20
MongoDB按照天數(shù)或小時(shí)聚合
需求
最近接到需求,需要對(duì)用戶賬戶下的設(shè)備狀態(tài),分別按照天以及小時(shí)進(jìn)行聚合,以此為基礎(chǔ)繪制設(shè)備狀態(tài)趨勢圖.
實(shí)現(xiàn)思路是啟動(dòng)定時(shí)任務(wù),對(duì)各用戶的設(shè)備狀態(tài)數(shù)據(jù)分別按照小時(shí)以及天進(jìn)行聚合,并存儲(chǔ)進(jìn)數(shù)據(jù)庫中供用戶后續(xù)查詢.
涉及到的技術(shù)棧分別為:Spring Boot
,MongoDB,Morphia
.
數(shù)據(jù)模型
@Data
@Builder
@Entity(value = “rawDevStatus”, noClassnameStored = true)
// 設(shè)備狀態(tài)索引
@Indexes({
// 設(shè)置數(shù)據(jù)超時(shí)時(shí)間(TTL,MongoDB根據(jù)TTL在后臺(tái)進(jìn)行數(shù)據(jù)刪除操作)
@Index(fields = @Field(“time”), options = @IndexOptions(expireAfterSeconds = 3600 * 24 * 72)),
@Index(fields = {@Field(“userId”), @Field(value = “time”, type = IndexType.DESC)})
})
public class RawDevStatus {
@Id
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private ObjectId objectId;
private String userId;
private Instant time;
@Embedded(“points”)
List<Point> protocolPoints;
@Data
@AllArgsConstructor
public static class Point {
/**
* 協(xié)議類型
*/
private Protocol protocol;
/**
* 設(shè)備總數(shù)
*/
private Integer total;
/**
* 設(shè)備在線數(shù)目
*/
private Integer onlineNum;
/**
* 處于啟用狀態(tài)設(shè)備數(shù)目
*/
private Integer enableNum;
}
}
網(wǎng)友評(píng)論