1

Docs:

{
  _id: 1,
  items: [{thing: 5}, {thing: 7}]
}
{
  _id: 2,
  items: [{thing: 5}, {thing: 11}]
}

I would like to remove all docs from the collection above if all elements in array have "thing" < 10. IE for that case doc 1 should be removed, doc 2 should remain.

Is it possible with a query to find only docs where all elements in the array match with a $lt query?

I tried this:

db.mycollection.remove({items: {$all: [{$elemMatch: {thing: {$lt: 11}}}]}})

However that will remove all docs if any of the elements in the array match the condition.

lostintranslation
  • 23,756
  • 50
  • 159
  • 262

1 Answers1

1

Use double negative (De-Morgan law):

{items: {$not: {$elemMatch: {thing: {$gte: 11}}}}}
Danny Varod
  • 17,324
  • 5
  • 69
  • 111