두 컬렉션에 대한 뷰를 생성하려면 $lookup 을(를) 사용하세요. 애플리케이션은 복잡한 파이프라인을 구성하거나 유지 관리하지 않고도 뷰를 쿼리 할 수 있습니다.
예시
..include:: /includes/sample-data-usage.rst
조인된 뷰 만들기
db.createView( "movieComments", "movies", [ { $match: { year: { $gte: 2014 } } }, { $lookup: { from: "comments", localField: "_id", foreignField: "movie_id", as: "movieComments" } }, { $project: { _id: 0, title: 1, year: 1, numComments: { $size: "$movieComments" } } } ] )
예시:
뷰 쿼리
댓글이 가장 많은 영화 5개에 대한 뷰를 쿼리합니다.
db.movieComments.aggregate( [ { $group: { _id: "$title", totalComments: { $sum: "$numComments" } } }, { $sort: { totalComments: -1 } }, { $limit: 5 } ] )
[ { _id: '<title>', totalComments: <num> }, { _id: '<title>', totalComments: <num> }, { _id: '<title>', totalComments: <num> }, { _id: '<title>', totalComments: <num> }, { _id: '<title>', totalComments: <num> } ]