dinesh oz
dinesh oz

Reputation: 342

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

Upvotes: 5

Views: 1306

Answers (2)

1sina1
1sina1

Reputation: 1086

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

Upvotes: 0

cmgchess
cmgchess

Reputation: 10247

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

Upvotes: 3

Related Questions