0

I have a MongoDB document like below, I would like to query values.mark greater than 50 and available=true and return documents when the values.available=true. If anyone of the value has available=False the whole document should not be returned.

{
  "_id": 1,
  "values": [
    {
      "mark": 30,
      "available": true
    },
    {
      "mark": 80,
      "available": false
    },
    {
      "mark": 65,
      "available": true
    }
  ]
}

Query:

{"values.mark": {"$gt":50}, 'values.available': true}

The result for the query

{
  "_id": 1,
  "values": [
    {
      "mark": 30,
      "available": true
    },
    {
      "mark": 80,
      "available": false
    },
    {
      "mark": 65,
      "available": true
    }
  ]
}

In the above case, one of the value for available is False so it should not return the document instead it should be empty. Can someone help me how to achieve the expected result?

1 Answer 1

1

Maybe something like this:

  db.collection.find({
    "values.mark": {
    $gt: 50
   },
    $nor: [
    {
      "values": {
        $elemMatch: {
          "available": {
            $nin: [
              true
            ]
          }
        }
      }
    }
  ]
})

playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.