简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM