Search code examples
mongodbmongo-shell

MongoDB toLowerCase()


I have a collection called Profiles, which contains an array called emails.

I want to change all the email strings in the array to lowercase.

db.Profiles.update({"emails":/@/i}, {$set: {"emails" : emails.toLowerCase()}}})

Error:

Tue Jan 29 16:52:28 SyntaxError: missing ) after argument list (shell):1

I also found this:

db.Profiles.find().forEach(
  function(e) {
    e.emails = e.emails.toLowerCase();
    db.Profiles.save(e);
  }
)

Error:

Tue Jan 29 16:51:41 TypeError: e.emails.toLowerCase is not a function (shell):3

Thanks in advance for any help.


Solution

  • You need to lowercase every string inside emails array. There's no toLowerCase function in string[].

    Example:

    db.Profiles.find().forEach(
       function(e) {
          for(var i = 0; i < e.emails.length; i++){ 
             e.emails[i] = e.emails[i].toLowerCase(); 
          }
          db.Profiles.save(e);  
    });