aggregation 처리 중 이전 stage 에서 나온 결과(배열A, 배열B)에서 배열A와 배열B를 합쳐서 새로운 배열을 만들어야 할 때 $concatArrays (aggregation) 오퍼레이터를 사용.


참고> https://docs.mongodb.org/manual/reference/operator/aggregation/concatArrays/


Posted by jungtae17
,

aggregation 처리 중 이전 stage 에서 나온 결과(배열)에서 새로운 배열을 만들어야 할 때 $map (aggregation) 오퍼레이터를 사용.



Posted by jungtae17
,

MongoDB 3.2 에서 추가된 $arrayElemAt (aggregation) 오퍼레이터를 사용해서 배열 요소를 꺼낼 수 있게 됨.


참고> https://docs.mongodb.org/manual/reference/operator/aggregation/arrayElemAt/


Posted by jungtae17
,

{

  "event": "tennis",

  "teams": [

    {

      "name": "Red",

      "players": [

        { "name": "Kim", "age": 22 },

        { "name": "Lee", "age": 21 }

      ]

    },

    {

      "name": "Blue",

      "players": [

        { "name": "Park", "age": 23 },

        { "name": "Choi", "age": 19 }

      ]

    }

  ]

}


* 배열안에 들어있는 데이터 찾기 ('Blue' 팀 찾기)

db.game.find(

  { teams: { $elemMatch: { name: "Blue" } } },

  { _id: 0, "teams.$": 1 }

);

  결과>

{

  "teams": [

    {

      "name": "Blue",

      "players": [

        { "name": "Park", "age": 23 },

        { "name": "Choi", "age": 19 }

      ]

    }

  ]

}


* 배열안 배열에서 데이터 찾기 ('Park' 플레이어 찾기)

db.game.find(

  { teams: { $elemMatch: { players: { $elemMatch: { name: "Park" } } } } },

  { _id: 0, "teams.$": 1 }

);


Posted by jungtae17
,