나는 이벤트라고 불리는 mongodb 콜렉션을 가지고 있는데, 여기에는 야구 게임이 들어있다. 나는 모든 팀의 순위를 반환하는 쿼리를 작성하는 데 도움이 필요MongoDB : 하위 문서의 배열에서 필드 집계
{
"name" : "Game# 814",
"dateStart" : ISODate("2012-09-28T14:47:53.695Z"),
"_id" : ObjectId("53a1b24de3f25f4443d9747e"),
"stats" : [
{
"team" : ObjectId("53a11a43a8de6dd8375c940b"),
"teamName" : "Reds",
"_id" : ObjectId("53a1b24de3f25f4443d97480"),
"score" : 17
},
{
"team" : ObjectId("53a11a43a8de6dd8375c938d"),
"teamName" : "Yankees",
"_id" : ObjectId("53a1b24de3f25f4443d9747f"),
"score" : 12
}
]
"__v" : 0
}
다음은 테이블의 한 레코드의 예입니다. 여기
{
"team" : ObjectId("53a11a43a8de6dd8375c938d"),
"teamName" : "Yankees",
"wins" : <<number of Yankees wins>>
"losses" : <<number of Yankees losses>>
"draws" : <<number of Yankees draws>>
}
{
"team" : ObjectId("53a11a43a8de6dd8375c940b"),
"teamName" : "Reds",
"wins" : <<number of Reds wins>>
"losses" : <<number of Reds losses>>
"draws" : <<number of Reds draws>>
}
...
내가 시작했습니다 쿼리의
이
db.events.aggregate(
{"$unwind": "$stats" },
{ $group : {
_id : "$stats.team",
gamesPlayed : { $sum : 1},
totalScore : { $sum : "$stats.score" }
}}
);
... 어떤 결과를 반환 ... :
{
"result" : [
{
"_id" : ObjectId("53a11a43a8de6dd8375c93cb"),
"gamesPlayed" : 125, // not a requirement... just trying to get $sum working
"totalScore" : 1213 // ...same here
},
{
"_id" : ObjectId("53a11a44a8de6dd8375c955f"),
"gamesPlayed" : 128,
"totalScore" : 1276
},
{
"_id" : ObjectId("53a11a44a8de6dd8375c9661"),
"gamesPlayed" : 152,
"totalScore" : 1509
},
....
지금까지 사용해 본 모든 것이 있습니까? 'db.events.aggregate ( : – peter
이 쿼리를 사용하여 { "$의 풀림을": "$ 통계를"}, {$ 그룹 : { _id : "$의 stats.team을" gamesPlayed : {$ 합계 : 1}, totalScore : {$ sum : "$ stats.score"} }} ); { "결과": [ { "_id": ObjectId가 ("53a11a43a8de6dd8375c93cb"), "gamesPlayed": 125, "totalScore"1213 ' 는이 같은 결과를 얻을 수 있어요 }, \t ... 하지만 다시 그룹화하는 방법을 알지 못하고 각 게임의 승패 상황을 파악합니다. – user3662896
서식을 보내 주셔서 감사합니다. blerg – user3662896