Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
Replace white space and format string

I can't find how to replace the whiteSpace in a aggregate request with MongoDB and how to delete a part of a string.(I'll probably need to use a regex).

Example of my document:

{
    "_id": "57dfa5c9xxd76b65426d5cca",
    "battle_id": "5744f01d1ad716a349c4999a",
    "is_first": false,
    "title": "division 1 #3"
},    
{
    "_id": "13dfa529dxd123x5426d5ccb",
    "battle_id": "1244x51d1aq546a349c423rcc",
    "is_first": true,
    "title": "division gold #5"
}

What I'm doing currently:

.project({
    "battle_id": "$battle_id",  
    "is_first": {
        $cond: [
            { $eq : ["$rank" , 1] }, 
            true, 
            false
        ]
    }, 
    "title": { $toLower: "$battle.title" }
})

Is it possible to change each white space by "_" character or do I need to do it in a forEach() after the request?

Moreover, is it possible to format the string to never return me the #3 for example?

Result that I need (changes are in title):

{
    "_id": "57dfa5c9xxd76b65426d5cca",
    "battle_id": "5744f01d1ad716a349c4999a",
    "is_first": false,
    "title": "division_1"
},    
{
    "_id": "13dfa529dxd123x5426d5ccb",
    "battle_id": "1244x51d1aq546a349c423rcc",
    "is_first": true,
    "title": "division_gold"
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can replace white space and format string by using Regular Expressions and Array#map() to get the result:

var arr = [{"_id": "57dfa5c9xxd76b65426d5cca","battle_id": "5744f01d1ad716a349c4999a","is_first": false,"title": "division 1 #3"},{"_id": "13dfa529dxd123x5426d5ccb","battle_id": "1244x51d1aq546a349c423rcc","is_first": true,"title": "division gold #5"}],
    result = arr.map(i => {
      return {
        '_id': i._id,
        'battle_id': i.battle_id,
        'is_first': i.is_first,
        'title': i.title.replace(/\s#[0-9]+/g, '').replace(/\s/,'_')
      };
    });

console.log(result);

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!