Reputation: 607
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.
Upvotes: 3
Views: 7792
Reputation: 1
db.collectionname.find({ "Key_name": {$ne:null} }).forEach(function(doc)
{
db.collectionname.update(
{ _id: doc._id },
{ "$set": { "Key_name": doc.Key_name.toLowerCase() } }
);
});
Upvotes: 0
Reputation: 524
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);
});
Upvotes: 1
Reputation: 116
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);
});
Upvotes: 5