MongoDB Aggregation Limit Lookup
Starting from MongoDB 3.6 you can use uncorrelated subqueries to limit the lookup:
db.indicators.aggregate([{ $lookup: { from: 'comments', as: 'match_docs', let: { indicator_id: '$_id' }, pipeline: [ { $match: { $expr: { $eq: [ '$obj_id', '$$indicator_id' ] } } }, // { $sort: { createdAt: 1 } }, // add sort if needed (for example, if you want first 100 comments by creation date) { $limit: 100 } ]} }])
I was able to figure it out.
$lookup -> $match -> $project
db.indicators.aggregate([{ "$lookup": { "from": "comments" , "localField": "_id" , "foreignField": "obj_id" , "as": "match_docs" }}, { "$match": { "match_docs": { "$exists": True } }}, { "$project": { "match_docs_agg": { "$slice": ["$match_docs", 3] } }}])
If you do a $unwind
immediately following a $lookup
, the pipeline will be optimized, basically combining the 2 stages helping to bypass the 16MB limit that could result from the $lookup
returning a large number of documents.
Keep in mind, if a single document in the foreign collection plus the size of the document in the local collection exceed 16 MB, this optimization cannot help.