skip to Main Content
  {
    "id": 1,
    "holdDetails": [
      {
        "holdDescription": "COVERT_LOCK",
        "holdStatus": "CREATED"
      },
      {
        "holdDescription": "ALPH_LOCK",
        "holdStatus": "RESOLVED"
      }
    ]
  },
  {
    "id": 2,
    "holdDetails": [
      {
        "holdDescription": "ALPHA_LOCK",
        "holdStatus": "RESOLVED"
      },
      {
        "holdDescription": "BETA_LOCK",
        "holdStatus": "RESOLVED"
      }
    ]
  },
  {
    "id": 3,
    "holdDetails": [
      {
        "holdDescription": "ALPHA_LOCK",
        "holdStatus": "CREATED"
      },
      {
        "holdDescription": "BETA_LOCK",
        "holdStatus": "CREATED"
      }
    ]
  }
]

Now I want to filter this document basis on a condition that get all of those object
where all of the holdDetails is in RESOLVED holdStatus(ie COVERT_HOLD also in RESOLVED
holdStatus) or all of them are in RESOLVED
holdStatus except COVERT_LOCK in CREATED status

So post this condition result should be

  {
    "id": 1,
    "holdDetails": [
      {
        "holdDescription": "COVERT_LOCK",
        "holdStatus": "CREATED"
      },
      {
        "holdDescription": "ALPH_LOCK",
        "holdStatus": "RESOLVED"
      }
    ]
  },
  {
    "id": 2,
    "holdDetails": [
      {
        "holdDescription": "ALPHA_LOCK",
        "holdStatus": "RESOLVED"
      },
      {
        "holdDescription": "BETA_LOCK",
        "holdStatus": "RESOLVED"
      }
    ]
  }
]

What should be the Query.class object created in java to produce such result?

2

Answers


  1. You can use $filter with $or for this:

    db.collection.aggregate([
      {$match: {
          $expr: {
            $eq: [
              {$size: "$holdDetails"},
              {$size: {
                  $filter: {
                    input: "$holdDetails",
                    cond: {
                      $or: [
                        {$eq: ["$$this.holdStatus", "RESOLVED"]},
                        {$eq: ["$$this.holdDescription", "COVERT_LOCK"]}
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    ])
    

    See how it works on the playground example

    Login or Signup to reply.
  2. You can try this:

    Query query = Query.query(where("holdDetails.holdStatus").is("RESOLVED"));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search