I was asked a question by a colleague today whether the MongoDB $filter aggregation stage is implemented efficiently.

The aggregation stage in question:

$project: {
    absences: {
        $filter: {
            input: "$$absences",
            as: "relevantAbsences",
            cond: {
                $and: [
                    { $eq: ["$absence.absenceType", AbsenceType.Sickleave] },
                    { $lte: ["$createdAt", "$$absence.created"] },
                    { $gte: ["$date", "$$absence.from"] },
                    { $lte: ["$date", "$$absence.to"] }

Question 1: Is there any public information on how MongoDB implements this operation? e.g. is it correct to assume that MongoDB iterates over the whole absences array for each document in the aggregation which is projected by the stage?

Question 2: If my assumption in Q1 is true: Do you know a solution how to achieve the same result with better performance - assuming that the absences-array is large (~ 1000 entries). There is another information which could make the query even faster: I only need to know one of the relevant absences, not all of them - I only need to know whether a relevantAbsence exists, i.e. relevantAbsences.size >= 1.

  • The C++ source code for the MongoDB server is on GitHub if you want to review the implementation. Since $filter selects a subset of an array matching the provided condition(s), it will have to iterate over the full input source for correctness. I expect what you are looking for is an $elemMatch projection, which returns the first matching result in an array. Can you edit your question to include an example document and the specific version of MongoDB server you are using? – Stennie Jul 12 '19 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.