MongoDB toLowerCase()

标签 mongodb mongo-shell

我有一个名为 Profiles 的集合,其中包含一个名为 emails 的数组。

我想将数组中的所有电子邮件字符串更改为小写。

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

错误:

1 月 29 日星期二 16:52:28 SyntaxError: missing ) after argument list (shell):1

我还发现了这个:

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

错误:

1 月 29 日星期二 16:51:41 TypeError:e.emails.toLowerCase 不是函数(shell):3

在此先感谢您的帮助。

最佳答案

您需要将电子邮件数组中的每个字符串小写。 string[] 中没有 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);  
});

关于MongoDB toLowerCase(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14588022/

相关文章:

java - 为什么我的 Mongo 聚合不能在嵌套文档上正常工作?

node.js - 获取 MongoDB 中特定月份和特定年份的文档

mongodb - 查找操作后隐藏嵌套文档中的 _id

bash - 在作为另一个命令一部分的Heredoc中解析变量

mongodb - 未检测到设置的主节点 [Mongo shell]

mongodb - 为启用身份验证的 mongodb 运行 mongostat 时出现问题

MongoDB 确定现有集合的分配策略

node.js - MongoDB + NodeJS : MapReduce or manual calculation

arrays - MongoDB 未正确查询给定位置数组内的 `null`

mongo-shell - 如何在 Linux mongosh 命令行上指定数据库以及身份验证