简体   繁体   中英

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?

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

Test code here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM