나는 각 사용자에 대한 각 유형의 경고의 수를 얻기 위해 MongoDB의 쉘에서 수행 다음 집계 한 : 나는 봄 데이터 MongoDB를로 번역 한Spring MongoDB 집계에서 프로젝트 DBRef는 어떻게됩니까?
db.getCollection('alerts').aggregate(
{
$unwind:"$son"
},
{
$group:
{
_id:{
son: "$son",
level: "$level"
},
count: { $sum: 1 }
}
},
{
$group:
{
_id:{
son: "$_id.son"
},
alerts: { $addToSet: {
level: "$_id.level",
count: "$count"
}}
}
}
)
다음과 같이
TypedAggregation<AlertEntity> alertsAggregation =
Aggregation.newAggregation(AlertEntity.class,
unwind("$son"),
Aggregation.group("$son", "$level").count().as("count"),
Aggregation.group("$_id.son")
.addToSet(new BasicDBObject("level", "$_id.level").append("count", "$count")).as("alerts"));
// Aggregation.match(Criteria.where("_id").in(sonIds)
AggregationResults<AlertsBySonDTO> results = mongoTemplate.
aggregate(alertsAggregation, AlertsBySonDTO.class);
List<AlertsBySonDTO> alertsBySonResultsList = results.getMappedResults();
return alertsBySonResultsList;
내가 명확하지 않고 작동하도록 할 수없는 것은 식별자와 가능한 경우 사용자의 이름 (아들 변수)을 투사하는 것입니다.
The resulting DTO is as follows
public final class AlertsBySonDTO implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty("identity")
private String id;
@JsonProperty("alerts")
private ArrayList<Map<String, String>> alerts;
}
그러나 id 속성에는 전체 내장 하위 엔티티가 포함됩니다.
이것은 경보 모음의 구조입니다.
{
"_id" : ObjectId("59e6ff3d9ef9d46a91112890"),
"_class" : "es.bisite.usal.bulltect.persistence.entity.AlertEntity",
"level" : "INFO",
"title" : "Alerta de Prueba",
"payload" : "Alerta de Prueba",
"create_at" : ISODate("2017-10-18T07:13:45.091Z"),
"delivery_mode" : "PUSH_NOTIFICATION",
"delivered" : false,
"parent" : {
"$ref" : "parents",
"$id" : ObjectId("59e6ff369ef9d46a91112878")
},
"son" : {
"$ref" : "children",
"$id" : ObjectId("59e6ff389ef9d46a9111287b")
}
}
/* 2 */
{
"_id" : ObjectId("59e6ff6d9ef9d46a91112892"),
"_class" : "es.bisite.usal.bulltect.persistence.entity.AlertEntity",
"level" : "WARNING",
"title" : "Token de acceso inv�lido.",
"payload" : "El token de acceso YOUTUBE no es v�lido",
"create_at" : ISODate("2017-10-18T07:14:53.449Z"),
"delivery_mode" : "PUSH_NOTIFICATION",
"delivered" : false,
"parent" : {
"$ref" : "parents",
"$id" : ObjectId("59e6ff369ef9d46a91112878")
},
"son" : {
"$ref" : "children",
"$id" : ObjectId("59e6ff389ef9d46a9111287b")
}
}
/* 3 */
{
"_id" : ObjectId("59e6ff6d9ef9d46a91112893"),
"_class" : "es.bisite.usal.bulltect.persistence.entity.AlertEntity",
"level" : "WARNING",
"title" : "Token de acceso inv�lido.",
"payload" : "El token de acceso INSTAGRAM no es v�lido",
"create_at" : ISODate("2017-10-18T07:14:53.468Z"),
"delivery_mode" : "PUSH_NOTIFICATION",
"delivered" : false,
"parent" : {
"$ref" : "parents",
"$id" : ObjectId("59e6ff369ef9d46a91112878")
},
"son" : {
"$ref" : "children",
"$id" : ObjectId("59e6ff389ef9d46a9111287c")
}
}
누구든지 내가이 접근 할 수있는 방법을 알고 :
JSON 포맷을 경고? MongoDB의 버전으로 미리
경고 컬렉션의 json 예제를 붙여 넣을 수 있습니까? –
붙여 넣은 이미지에서 아들이 배열 인 것을 볼 수 없습니다. 집계가 풀리기 때문에 배열입니다. 이미지 대신 json을 붙여 넣는 것이 좋습니다. –
MongoDB에서 집계를 실제로 마스터하지는 않습니다. 원하는 결과를 얻는 유일한 방법입니다. 그러나 제 질문은 투사의 마지막 부분에 더 초점을 맞추고 있습니다. DBRef 필드의 ID를 가져올 수 없습니다. –