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?
CodePudding user response:
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 thelabels
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 oflabels
array and trim thename
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 }
)