Reputation: 1577
I have the following document in my MongoDB:
{
name: "BRIAN"
}
I'm trying to change it to this format:
{
name: "Brian"
}
I've tried the following:
{
$project: {
name: { $concat: [ { $toLower: '$name' } ]}
}
}
So this makes the string lowercase, then I just need to make the first letter uppercase. Is it possible to do that within the same project aggregation?
Upvotes: 0
Views: 468
Reputation: 15237
As @Wernfried Domscheit suggested, you can use $substrCP
to segment name
and perform $toLower
separately for the "tail" and keep the first character upper case. Use $concat
to put back the 2 results together.
db.collection.update({},
[
{
"$addFields": {
"name": {
"$concat": [
{
"$substrCP": [
"$name",
0,
1
]
},
{
"$toLower": {
"$substrCP": [
"$name",
1,
{
"$strLenCP": "$name"
}
]
}
}
]
}
}
}
],
{
multi: true
})
Upvotes: 2