K
K
killkock2017-05-28 23:23:27
MongoDB
killkock, 2017-05-28 23:23:27

MongoDB how to check all occurrences of an array?

I have a db with users like this:

{
"_id": 1,
"name": "Carlo",
"cars": [{
    "type": "sport",
    "score": 10
}, {
    "type": "hatchback",
    "score": 5
}, {
    "type": "family",
    "score": 4
}, {
    "type": "family",
    "score": 3
}]
}

I need to select users whose all machine types have a score greater than 5 and less than 8. I wrote the following code:
db.bsa.find({
  $and: [{  
    'cars.score': {
      $gt: 5
    }
  }, {
    'cars.score': {
      $lt: 8
    }
  }]
})

But such a query returns all users who have at least one machine with this account. How can I return only those for which all machines match the query?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question