Hi Mark,
Seeking assistance on how we could update all to be Title Case (First letter of each word in upper case, rest in lower case)
One way to achieve this is to define a Javascript function that returns a title case in the mongo shell, e.g.:
function titleCase(x) {
return x.split(' ').map(function(x) {
return x[0].toUpperCase() + x.slice(1).toLowerCase()
}).join(' ')
}
Then call the titleCase() function for each document in the collection, saving each modified document back into the collection. For example, using MongoDB 3.2.11:
> db.test.insert({a:'The Cat Jumped Down the Road'})
> db.test.insert({a:'THE DOG SAT ON THE LOG'})
> // Call the titleCase() function for each document, and save the new document
> db.test.find().forEach(function(doc) {
... doc.a = titleCase(doc.a)
... db.test.save(doc)
... })
Updated 1 existing record(s) in 1ms
Updated 1 existing record(s) in 1ms
> db.test.find()
{
"_id": ObjectId("5833b3ac4e2ef0aa79003885"),
"a": "The Cat Jumped Down The Road"
}
{
"_id": ObjectId("5833b3ad4e2ef0aa79003886"),
"a": "The Dog Sat On The Log"
}
Please note that the code snippet above was not extensively tested and only serves as an illustration of how this can be achieved in the mongo shell. Before doing any major maintenance which involves every document in your collection, please make sure that every procedure was tested and all backups are recent.
Best regards,
Kevin