0

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?

user15780176
  • 109
  • 8

1 Answers1

1

Query

  • pipeline update requires MongoDB >= 4.2
  • because you want to split on first index of "-" you can do it with out splitting in all "-" occurences
  • the bellow finds the index of "-" the left part is the MGESAD and the right is the MGE_DESC

*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})
Takis
  • 8,314
  • 2
  • 14
  • 25