5

Description:

I want to only return if all the objects in the array match the condition. Now, it's returning the object if at least one condition matches in the array of objects.

If you guys have any more queries regarding this question. I will answer please ask!

Input:

[
   {
      "_id":"4",
      "intends":[
         {
            "_id":"1",
            "status":"Packed"
         },
         {
            "_id":"2",
            "status":"Packed"
         }
      ]
   },
   {
      "_id":"5",
      "intends":[
         {
            "_id":"3",
            "status":"Packed"
         },
         {
            "_id":"4",
            "status":"Created"
         }
      ]
   }
]

Current Output:

[
   {
      "_id":"4",
      "intends":[
         {
            "_id":"1",
            "status":"Packed"
         },
         {
            "_id":"2",
            "status":"Packed"
         }
      ]
   },
   {
      "_id":"5",
      "intends":[
         {
            "_id":"3",
            "status":"Packed"
         },
         {
            "_id":"4",
            "status":"Created"
         }
      ]
   }
]

Expected Output:

[
   {
      "_id":"4",
      "intends":[
         {
            "_id":"1",
            "status":"Packed"
         },
         {
            "_id":"2",
            "status":"Packed"
         }
      ]
   }
]

I have tried:

db.collection.find({intends.status: "Packed"})
db.collection.find({intends: {$elemMatch: {status: "Packed"}}})
4
  • 1
    so you need records of when every value of intends has status Packed? Commented Mar 8, 2022 at 13:56
  • Exactly I want that. I also posted the expected result. Commented Mar 8, 2022 at 14:00
  • FYI, Have also tried with $elemMatch... Please check the updated question. Commented Mar 8, 2022 at 14:14
  • 1
    updated answer. use ne instead of nin since you are only searching one value Commented Mar 8, 2022 at 14:24

2 Answers 2

4

A possible solution using $elemMatch, $nin and $not.
The $elemMatch, $nin part will give all elements where there is at least one element with status not "Packed". So the $not will reverse it and give elements where every status is "Packed"

db.collection.find({
  intends: {
    "$not": {
      "$elemMatch": {
        status: {
          "$nin": [
            "Packed"
          ]
        }
      }
    }
  }
})

demo

UPDATE Since here just checking 1 value use $ne instead of $nin

db.collection.find({
  intends: {
    $not: {
      $elemMatch: {
        status: {
          $ne: "Packed"
        }
      }
    }
  }
})

demo

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

Comments

0

using aggregation $redact and $allElementsTrue

test it at mongoPlayground

db.collection.aggregate([
  {
    "$redact": {
      "$cond": [
        {
          "$allElementsTrue": {
            "$map": {
              "input": "$intends",
              "as": "intend",
              "in": {
                "$eq": [
                  "$$intend.status",
                  "Packed"
                ]
              }
            }
          }
        },
        "$$KEEP",
        "$$PRUNE"
      ]
    }
  }
])

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.