【问题标题】:MongoDB - How to add findandmodify privilege to a userMongoDB - 如何向用户添加查找和修改权限
【发布时间】:2015-09-07 13:05:55
【问题描述】:

在我的应用程序中,我使用 MongoDB 作为数据库,Express + Mongoose 用于处理代码。我已经向数据库添加了一个用户并为该用户赋予了readWrite 角色,现在我可以从数据库中获取数据。但是当我尝试在Express + Mongoose 中使用save 函数插入集合时,我收到以下错误消息,并且找不到如何将findandmodify 权限添加到用户。

 {"name":"MongoError",
   "ok":0,
   "errmsg":"not authorized on od-database to execute  command { findandmodify: \"identitycounters\", 
   query: {
      field: \"id\",  
      model: \"Products\" },
   new: 1 , 
   remove: 0, 
   upsert: 0,
   update: { $inc: { count: 1 } } }","code":13}

请帮帮我。

【问题讨论】:

  • 请分享显示数据库名称、您如何调用mongoose.createConnection 以及您如何授予用户权限的代码片段。谢谢!
  • 请在后台显示您的代码,我们将帮助您解决此问题。

标签: mongodb express mongoose


【解决方案1】:

我测试一个readWrite角色的用户连接mongoDB对非系统文件进行save和findAndModify操作,都可以成功。 p>

请确认

  • mongoose.createConnction或mongoose.connect中连接数据库的正确用户名和密码。
  • 此用户确实存在于db.system.users 和readWrite 角色中。
  • readWrite 用户只能修改system.js 集合中所有非系统 集合上的数据

【讨论】:

    【解决方案2】:

    尝试给他dbAdmin 角色。

    【讨论】:

      【解决方案3】:

      或者尝试禁用授权或添加连接到数据库的用户。
      将用户添加到数据库的示例:

      db.createUser({
         user: "admin", 
         pwd: "test", 
         roles: [ { role: "dbAdmin", db: "shop" } ] 
      })
      

      地点:

      • db:"shop" - 是名称数据库
      • pwd: "test" - 密码数据库
      • user: "admin" - 连接数据库的用户,例如db.auth('admin', 'test')

      【讨论】:

        【解决方案4】:

        如果为findAndModify 指定bypassDocumentValidation,则需要dbAdmin 角色或具有bypassDocumentValidation 权限的自定义角色。

        布尔参数最好指定为true 或false,而不是0 和1

        【讨论】:

          猜你喜欢
          • 2013-05-07
          • 2020-08-13
          • 2019-03-24
          • 1970-01-01
          • 2019-12-13
          • 2018-10-29
          • 2015-07-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多