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

520
Views
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 answers
Answer question

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 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!