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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s