1

sample value in DB: one-two-three.js

Need to format like this: one two three

I split the field using $split aggregate, but after this how to replace "-" with " " ?

replaceAll("-", " ")

$push: {
  name: {$arrayElemAt:[{$split: ["$name" , "."]}, 0]},
}

1 Answer 1

1

You can try,

  • your tried code, $set, $split name with . and get first element using $arrayElemAt
  • $reduce to iterate loop of name array after converting form string using $split
  • $concat to concat string with space and return value, this will return with extra space in first position of string, and $substr will remove that first position space
  {
    $set: {
      name: {
        $arrayElemAt: [{ $split: ["$file", "."] }, 0]
      }
    }
  },
  {
    $set: {
      name: {
        $substr: [
          {
            $reduce: {
              input: { $split: ["$name", "-"] },
              initialValue: "",
              in: { $concat: ["$$value", " ", "$$this"] }
            }
          },
          1,
          -1
        ]
      }
    }
  }

Playground

Sign up to request clarification or add additional context in comments.

2 Comments

But, it doesn't split the ".js"
that you have already did in your question, i have merged both in answer, you can check..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.