mongodbaggregation-frameworkstudio3t

How to split a column based on first occurrence of a delimter MongoDB


I have A column like this and it should be split based on the first "-", example is below

MGESAD :
"6095 - NCAM - US - GIUTCB - US Consumer Bank - USRB"
"6595 - NBAM - US - UDAS - Consumer Bank - USRB"
"0595 - NWWAM - US - GWCB - US BANK Bank - USRB - TBL"

I need to split this column into:

Col1  Col2
6095  NCAM - US - GIUTCB - US Consumer Bank - USRB
6595  NBAM - US - UDAS - Consumer Bank - USRB
0595  NWWAM - US - GWCB - US BANK Bank - USRB - TBL

Tried So far:

db.getCollection("arTes").aggregate([
    {
        $addFields: { 
            MGE_ID: { $arrayElemAt: [ { "$split": [ "$MGESAD y", "-"] }, 0 ] },
            MGE_DESC: { $arrayElemAt:[{ "$split": [ "$MGESAD ", "-"] },2] }
        }
    }
])

MGE_DESC is giving only 2 element I need entire string excluding the first split.

Let me know if there is any eaiser way to do this?


Solution

  • Query

    *if you only want to aggregate, use the pipeline ["$set" ...] in aggregation

    *if you wanted to do this not for the first or last "-" you could split and then $concat and maybe $reduce depending on your needs but here its more simple so those weren't used

    PlayMongo

    updade({},
    [{"$set": 
       {"MGESAD": 
         {"$substrCP": ["$MGESAD", 0, {"$indexOfCP": ["$MGESAD", " - "]}]},
          "MGE_DESC": 
          {"$substrCP": 
            ["$MGESAD",
             {"$add": [{"$indexOfCP": ["$MGESAD", " - "]}, 3]},
             {"$strLenCP": "$MGESAD"}]}}}],
    {"multi" : true})