【问题标题】:MongoDB mass update is not working [closed]MongoDB大规模更新不起作用[关闭]
【发布时间】:2023-03-27 22:50:02
【问题描述】:

我正在尝试通过覆盖表中的所有电子邮件值来清理一组数据(通过mongodb reference here)。以下不会导致错误,但也不会更新任何值:

db.applicants.update(
    { institution_id: { $gt: 1 } }, 
    {$set: {email: "sanitizedmail@there.com"}},
    { multi: true })

和样本记录:

{
    "id": "0003ee8c-2288-11e4-9610-0015c5f288ee",
    "created_at": "8/12/14 9:24",
    "updated_at": "8/12/14 9:25",
    "email": "some@there.com",
    "institution_id": "379",
}

【问题讨论】:

标签: mongodb-query


【解决方案1】:

根据你的数据结构

机构 ID

field 是 type: text (2),所以 $gt 将无法行动。

使用下面提供的 sn-p 将其更改为整数:

db.applicants.find( { 'institution_id' : { $type : 2 } } ).forEach( 
    function (x) {   
      x.institution_id = parseInt(x.institution_id); // convert field to int
      db.applicants.save(x);
})

【讨论】:

  • 非常感谢。这做到了。没有意识到该列被记录为文本。
猜你喜欢
  • 2019-05-18
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 2014-08-24
  • 2018-08-15
相关资源
最近更新 更多