aggregation 의 $project 스테이지에서 필드값이 아닌 상수값을 지정할 수 있다.


아래와 같이 $literal 오퍼레이터를 이용한다.


db.statistics.aggregate([
  { $match: { _id: { $lte: 1000 } } },
  { $project: { _id: 0, "Tags": { $literal: "User" } } }
]);


Tags 필드 값은 "User"(문자열)으로 반환된다.



Posted by jungtae17
,

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 의 find() 쿼리 후 결과의 필드 이름을 바꾸고 싶을다면 Aggregation 을 이용하자.


aggregation 의 $project 스테이지에서 필드 이름을 지정할 수 있다.

단, 먼저 쿼리($match 스테이지) 실행한 결과에서 $project 스테이지를 실행한다.


db.game.aggregate([

  { $match: { _id: { $lte: 1000 } } },

  { $project: { _id: 0, "Rank": "$_id", Name: 1 } }

]);


위 코드는 '_id' 필드를 'Rank' 필드로 이름 바꿔서 결과를 반환한다.


참고로 _id 필드 이름을 실제로 바꾸는 것이 아니기 때문에 _id 필드를 포함하지 않도록 해야된다.


Posted by jungtae17
,

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


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


Posted by jungtae17
,

MongoDB 3.2 에서 추가된 $lookup (aggregation) 오퍼레이터를 사용해서 Join 처리를 할 수 있게 됨.


참고1> https://www.mongodb.com/blog/post/joins-and-other-aggregation-enhancements-coming-in-mongodb-3-2-part-1-of-3-introduction


참고2> https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/#pipe._S_lookup


Posted by jungtae17
,