Hi I came across with this discussion where it was advised to limit the lookup only to the first occurrence.
It got me wondering if I have a lookup with a limit set to one:
{
$lookup: {
from: collection_name,
let: { idToSearchFor: '$_id' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$$idToSearchFor', '$another_id'] },
{ $eq: ['dummy', '$another_field'] },
],
},
},
},
{ $limit: 1 },
],
as: 'nice_name',
},
},
Is Mongo clever enough to terminate after the first match, or still will search through the whole collection, collecting all the matches and then returning with the very first one?
If it is searching through the whole collection, is there a way to terminate after the first match and save the time spent on looking through all the documents?
Many thanks in advance!