Mongoose – Return object only if condition matches all the objects in array

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"}}})

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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"
      ]
    }
  }
])


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x