How to use match as with Regular Expression in Mongodb with in Aggregate switch case?

0

Issue

Here what i did.

Inside $AddFields

    {
 ClientStatus:{
      $switch: {
   branches: [
      { case: {
        $eq:
        [
          "$CaseClientStatus",
           /In Progress/i              
        ]},
          then:'In Progress'
      },
      { case: {
        $eq:
        [
          "$CaseClientStatus",
          {regex:/Cancelled/i}
        ],
      },then:'Cancelled'},
      { case: {$eq:['$CaseClientStatus','Complete - All Results Clear']}, then:'Complete'},
      { case: {$eq:['$CaseClientStatus','Case on Hold']}, then:'Case on Hold'}
   ],
   default: 'Other'
}}
}

but with this my ClientStatus is showing only Complete,Other,Case On Hold not the one with specified with regex. alghough field contains those words.

here is the one of the doc

{
  "CandidateName": "Bruce  Consumer",  
  "_id": "61b30daeaa237672bb7a17cc",
  "CaseClientStatus": "Background Check Case In Progress",
  "TAT": "N/A",
  "CaseCloseDate": null,
  "FormationAutomationStatus": "Automated",
  "MethodOfDataSupply": "Automated",
  "Status": "Background Case In Progress",
  "CreatedDate": "2021-12-10T08:19:58.389Z",  
  "OrderId": "Ord3954",
  "PONumber": "",
  "Position": "",
  "FacilityCode": "",
  "IsCaseClose": false,
  "Requester": "Shah Shah",
  "ReportErrorList": 0
}

Solution

Assuming you are on version 4.2 or higher (and you should be because 4.2 came out almost 2 years ago) then the $regexFind function gives you what you need. Prior to 4.2, regex was only available in a $match operator, not in complex agg expressions. Your attempt above is admirable but the // regex syntax is not doing what you think it should be doing. Notably, {regex:/Cancelled/i} is simply creating a new object with key regex and string value /Cancelled/i (including the slashes) which clearly will not equal anything in $CaseClientStatus. Here is a solution:

ClientStatus:{
    $switch: {
      branches: [
         { case: {
            $ne: [null, {$regexFind: {input: "$CaseClientStatus", regex: /In Progress/i}}]
           }, then:'In Progress'},
         { case: {
            $ne: [null, {$regexFind: {input: "$CaseClientStatus", regex: /Cancelled/i}}]
           },then:'Cancelled'},
         { case: {$eq:['$CaseClientStatus','Complete - All Results Clear']}, then:'Complete'},
         { case: {$eq:['$CaseClientStatus','Case on Hold']}, then:'Case on Hold'}
      ],
      default: 'Other'
}}

It looks like you are trying to take a somewhat free-form status "description" field and create a strong enumerated status from it. I would recommend that your $ClientStatus output be more code-ish e.g. IN_PROGRESS, COMPLETE, CXL etc. Eliminate case and certainly whitespace.

Answered By – Buzz Moschetti

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More