I have an existing MongoDB collection containing user names. The user names contain both lower case and upper case letters.
I want to update all the user names so they only contain lower case letters.
I have tried this script, but it didn’t work
db.myCollection.find().forEach(
function(e) {
e.UserName = $toLower(e.UserName);
db.myCollection.save(e);
}
)
MongoDB does not have a concept of
$toLoweras a command. The solution is to run a bigforloop over the data and issue the updates individually.You can do this in any driver or from the shell:
You can also replace the save with an atomic update:
Again, you could also do this from any of the drivers, the code will be very similar.
EDIT: Remon brings up a good point. The
$toLowercommand does exist as part of the aggregation framework, but this has nothing to do with updating. The documentation for updating is here.