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

522
Vistas
MongoDB - Removing leading and trailing spaces from array of objects

I have a users collection in MongoDB with the following structure (I removed all irrelevant fields):

{
    _id: ObjectId,
    labels: Array <{ name: String, category: String }>
}

I want to remove leading and trailing spaces from the name property inside the labels array. I tried to do this:

db.users.update(
  { "labels.name" : { $regex: /^\s+|\s+$/ } }, 
  [{ $set: { "labels.$[].name": { $trim: { input: "$labels.$[].name" } } } }],
  { multi: true }
)

But I'm getting the following error:

{
    "message" : "write failed with error: {" +
              "    'nMatched' : 0," +
              "    'nUpserted' : 0," +
              "    'nModified' : 0," +
              "    'writeError' : {" +
              "    \t'code' : 16410," +
              "    \t'errmsg' : 'Invalid $set :: caused by :: FieldPath field names may not start with '$'. Consider using $getField or $setField.'" +
              "    }" +
              "}",
    "stack" : "script:4:2"
}

What is the correct way to solve this?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can not use aggregation operators ($trim) in regular update query, you can try update with aggregation pipeline starting from MongoDB 4.2,

  • $regex operator to pass ^ to match space from beginning and $ to match space from end. Note this will not pinpoint names in the labels array with leading or trailing space, but it will eliminate from processing any doc where none of the names have leading or trailing space.
  • put array bracket in update part
  • $map to iterate loop of labels array and trim the name using $trim operator
db.users.update(
  { "labels.name": { $regex: "^ | $" } },
  [{
    $set: {
      labels: {
        $map: {
          input: "$labels",
          in: {
            category: "$$this.category",
            name: { $trim: { input: "$$this.name" } }
          }
        }
      }
    }
  }],
  { multi: true }
)

Playground

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