MongoDB toLowerCase()
我有一个名为Profiles的集合,其中包含一个名为email的数组 我想将数组中的所有电子邮件字符串更改为小写MongoDB toLowerCase(),mongodb,mongo-shell,Mongodb,Mongo Shell,我有一个名为Profiles的集合,其中包含一个名为email的数组 我想将数组中的所有电子邮件字符串更改为小写 db.Profiles.update({"emails":/@/i}, {$set: {"emails" : emails.toLowerCase()}}}) 错误: 1月29日星期二16:52:28语法错误:缺少)在参数列表(shell)之后:1 我还发现: db.Profiles.find().forEach( function(e) { e.emails = e.
db.Profiles.update({"emails":/@/i}, {$set: {"emails" : emails.toLowerCase()}}})
错误:
1月29日星期二16:52:28语法错误:缺少)在参数列表(shell)之后:1
我还发现:
db.Profiles.find().forEach(
function(e) {
e.emails = e.emails.toLowerCase();
db.Profiles.save(e);
}
)
错误:
星期二1月29日16:51:41类型错误:e.emails.toLowerCase不是函数(shell):3
提前感谢您的帮助。您需要将电子邮件数组中的每个字符串小写。字符串[]中没有toLowerCase函数 例如:
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);
});
db.Profiles.find().forEach(
职能(e){
对于(var i=0;i
我找到了一种更简单、更快的方法
db.Profiles.find({email : { $exists: true}}).forEach(
function(e) {
e.email = e.email.toLowerCase();
db.Profiles.save(e);
});
你能解释一下为什么这个代码能回答这个问题吗?代码唯一的答案是,因为它们不教解决方案。@JohnAllen此示例适用于数组
db.collectionname.find({ "Key_name": {$ne:null} }).forEach(function(doc)
{
db.collectionname.update(
{ _id: doc._id },
{ "$set": { "Key_name": doc.Key_name.toLowerCase() } }
);
});