2016-06-07 3 views
0

내가 좋아 내 컬렉션의 문서가 : 내가 활동의 총 수를 얻을 수 있습니다- 포함 된 문서 MongoDB를

{ 
    _id: 1, 
    activities: [ 
    { 
     activity_id: 1, 
     travel: [ 
     { 
      point_id: 1, 
      location: [-76.0,19.1] 
     }, 
     { 
      point_id: 2, 
      location: [-77.0,19.3] 
     } 
     ] 
    }, 
    { 
     activity_id: 2, 
     travel: [ 
     { 
      point_id: 3, 
      location: [-99.3,18.2] 
     } 
     ] 
    } 
    ] 
}, 
{ 
    _id: 2, 
    activities: [ 
    { 
     activity_id: 3, 
     travel: [ 
     { 
      point_id: 4, 
      location: [-75.0,11.1] 
     } 
     ] 
    } 
    ] 
} 

을 다음과 같이

db.mycollection.aggregate(
    {$unwind: "$activities"}, 
    {$project: {count:{$add:1}}}, 
    {$group: {_id: null, number: {$sum: "$count" }}} 
) 

I (3 활동)을 얻을 :

{ "result" : [ { "_id" : null, "number" : 3 } ], "ok" : 1 } 

질문 : 호 모든 여행에서 총 요소 수를 얻을 수 있습니까?

예상 된 결과 :4 요소

이 있습니다

{ 
    point_id: 1, 
    location: [-76.0,19.1] 
}, 
{ 
    point_id: 2, 
    location: [-77.0,19.3] 
}, 
{ 
    point_id: 3, 
    location: [-99.3,18.2] 
}, 
{ 
    point_id: 4, 
    location: [-75.0,11.1] 
} 
+1

이 있었다 여전히 db.mycollection.aggregate'로 간단는 ({ "$ 그룹 ": {"_id ": null,"total ": {"$ sum ": {"$ sum ": {"$ map ": {"input ":"$ activities ","as ":"a " "in": { "$ size": "$$ a.travel"}}}}}}}})'. 왜냐하면'$ sum'은 MongoDB 3.2 이후 누적 기가 될뿐만 아니라 배열에서도 직접 작동 할 수 있기 때문입니다. 자신의 삭제 된 답변에 의해 현재 MongoDB 버전은 그보다 훨씬 오래되었다. –

답변

1

쉽게 더블 $unwind

예를 들어, 사용하여 문서를 변환 할 수 있습니다

db.collection.aggregate([ 
    {$unwind: "$activities"}, 
    {$unwind: "$activities.travel"}, 
    {$group:{ 
    _id:null, 
    travel: {$push: { 
     point_id:"$activities.travel.point_id", 
     location:"$activities.travel.location"}} 
    }}, 
    {$project:{_id:0, travel:"$travel"}} 
]) 

이 원하는 출력 형식에 매우 근접하다 방출됩니다

{ 
    "travel" : [ 
     { 
      "point_id" : 1.0, 
      "location" : [ 
       -76.0, 
       19.1 
      ] 
     }, 
     { 
      "point_id" : 2.0, 
      "location" : [ 
       -77.0, 
       19.3 
      ] 
     }, 
     { 
      "point_id" : 3.0, 
      "location" : [ 
       -99.3, 
       18.2 
      ] 
     }, 
     { 
      "point_id" : 4.0, 
      "location" : [ 
       -75.0, 
       11.1 
      ] 
     } 
    ] 
} 

업데이트 :

당신은 단지 전체 컬렉션의 여행 서류의 총 수를 알고 싶다면,

try :

db.collection.aggregate([ 
    {$unwind: "$activities"}, 
    {$unwind: "$activities.travel"}, 
    {$group: {_id:0, total:{$sum:1}}} 
]) 

그것은 인쇄됩니다 :

OP 통합 프레임 워크의 일부 속성을 기준으로 문서를 필터링하고 싶어 : 2

{ 
    "_id" : NumberInt(0), 
    "total" : NumberInt(4) 
} 

업데이트. 그것은 인쇄 (샘플 문서 기준)됩니다

db.collection.aggregate([ 
    {$unwind: "$activities"}, 
    {$match:{"activities.activity_id":1}}, 
    {$unwind: "$activities.travel"}, 
    {$group: {_id:0, total:{$sum:1}}} 
]) 

을 : 여기에 그렇게 할 수있는 방법입니다 실제로

{ "_id" : 0, "total" : 2 } 
+0

이것은 완벽합니다. 고맙습니다. ..... 두 번'$ unwind'에 대해 모르겠습니다. –

+0

은'{$ match : { "activities.activity_id": 1}}'매개 변수를 추가 할 수 있습니다. 특정 활동의 여행 목록? .... 그것을하는 방법? –

+0

실제로 그렇게 할 수 있습니다. $ match – Saleem