Home > Blockchain >  mongoose get only those documents if all subdocuments pass the criteria
mongoose get only those documents if all subdocuments pass the criteria

Time:12-09

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?

CodePudding user response:

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

  • Related