Pergunta

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.

Foi útil?

Solução

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);  
});

Outras dicas

I found an easier and faster method

db.Profiles.find({email : { $exists: true}}).forEach(
 function(e) {
   e.email = e.email.toLowerCase();
    db.Profiles.save(e);
 });
db.collectionname.find({ "Key_name": {$ne:null} }).forEach(function(doc) 
{
    db.collectionname.update(
        { _id: doc._id },
        { "$set": { "Key_name": doc.Key_name.toLowerCase() } }
    );
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top