【问题标题】:Delete a key in all the documents删除所有文档中的一个键
【发布时间】:2018-05-27 20:22:53
【问题描述】:

我通过删除键(即ips)修改了架构(即users)。因此我想在数据库中的所有文档中删除这个键。

例如,在mongo consoleRobo 3T 中,db.getCollection('users').find({}) 返回所有用户。其中一些包含密钥ips。有谁知道如何在控制台或 Robo 3T 中删除 ips

【问题讨论】:

  • 试试db.getCollection('users').update( { }, { $unset: { ips: "" } } )
  • 你确定吗?我刚测试,没用……之前ips被定义为[{ type: String }]@Veeram
  • 试试这个db.getCollection('users').update({},{ips : undefind})
  • usersips的数据类型是什么?
  • ips[{ type: String }]users 是一个包含多个键的对象,包括 ips

标签: mongodb mongoose robo3t


【解决方案1】:

更新多个文档

要更新多个文档,请将multi 选项设置为true。参考here

db.getCollection('users').update(
  { },
  { $unset: { ips: 1 } },
  { multi: true } 
)

【讨论】:

    【解决方案2】:

    正如@Veeram 已经发布的那样,您可以使用$unset 运行定期更新,只需在更新all 文档的选项中添加multi: true,否则它只会更新一个

    db.users.update(
      { }, // where
      { $unset: { ips: 1 } }, // change what
      { multi: true } // options
    )
    

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 1970-01-01
      • 2021-03-09
      • 2012-09-05
      • 2020-07-04
      • 2011-05-28
      • 2012-05-15
      相关资源
      最近更新 更多