MongoDB: How to make all fields of a collection lowercase

This simple script has been tested on MongoDB 3.2. It should work on older versions of MongoDB as well.

db.collectionName.find( {}, { 'fieldName': 1 } ).forEach(function(doc) {
db.collectionName.update(
   { _id: doc._id},
   { $set : { 'fieldName' : doc.fieldName.toLowerCase() } },
   { multi: true }
)
});

To convert all fields inside of an array, use this trick… MongoDB: How to lower case all items of an array

Leave a comment