Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda