Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

521
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda