Home > Net >  Mongoose - Return object only if condition matches all the objects in array
Mongoose - Return object only if condition matches all the objects in array

Time:03-08

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

CodePudding user response:

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

CodePudding user response:

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