• Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Post job
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

125
Views
mongoose get only those documents if all subdocuments pass the criteria

Let's say I have a Lesson schema and it contains an array of question subdocuments. How would I get those lessons where it is marked as completed but all the questions in the array have isDeleted: true

I thought of something like

Lesson.find({ isComplete: true, 'questions.isDeleted': true })

but that gets the lessons if at least one of the questions is deleted. How would I assert the condition for all of the questions?

over 1 year ago · Santiago Trujillo
1 answers
Answer question

0

You can't use the dot notation as it flattens the array, What you can do is use $expr, this allows the use of aggregation operators, like $reduce.

Now we can iterate over the array and see if all the documents satisfy the conditions, like so:

db.collection.find({
  isComplete: true,
  $expr: {
    $eq: [
      {
        $reduce: {
          input: "$questions",
          initialValue: 0,
          in: {
            $sum: [
              "$$value",
              {
                $cond: [
                  {
                    $ne: [
                      "$$this.isDeleted",
                      true
                    ]
                  },
                  1,
                  0
                ]
              }
            ]
          }
        }
      },
      0
    ]
  }
})

Mongo Playground

over 1 year ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.