0

I have the following schema:

var ParagraphsSchema = new Schema({
originalText: String,
suggestions: [{
    text: String,
    is_approved: { type: Boolean, default: false }
  }]
});

And the task is to get paragraphs where all suggestions are not approved (suggestion.is_approved = false)

I have already tried things like

Paragraph.find({ 'suggestions.is_approved': { $all: [false] })

Paragraph.find({'suggestions.is_approved': { $all: {"$elemMatch": false} } })

but none of them seem to work.

The obvious way would be to add "is_approved" property to Paragraph schema, but that would make data redundant and make code harder to maintain, that's why I would like to avoid it.

I'm using MongoDB==3.6.2 and Mongoose==5.0.3

1 Answers1

0

So, I found the solution that works for me (thanks to @Veeram's answer in comments). Here it is:

Paragraph.find({ 
       suggestions: { $gt: [] }, 
      'suggestions.is_approved':  { $ne: true } 
})

I added suggestions: { $gt: [] } to get rid of paragraphs where suggestions array is empty.