Sherin Jose
Sherin Jose

Reputation: 2526

MongoDB - How to add findandmodify privilege to a user

In my application I am using MongoDB as the database and Express + Mongoose for handling the codes. I have added a user to the database and given the readWrite role for the user, now I can fetch the data from the database. But when I am trying to insert a collection using the save function in Express + Mongoose I got the following error message and I couldn't find how to add the findandmodify privilege to the user.

 {"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}

Please help me.

Upvotes: 9

Views: 1768

Answers (4)

Gert van den Berg
Gert van den Berg

Reputation: 2756

If bypassDocumentValidation is specified for findAndModify, the dbAdmin role, or a custom role with the bypassDocumentValidation permission is needed.

The boolean parameters should preferably be specified as true or false instead of 0 and 1 as well

Upvotes: 0

zangw
zangw

Reputation: 48376

I test one user with readWrite role to connect mongoDB do the save and findAndModify operation on non-system document, both of them could be done successfully.

Please make sure

  • The correct username and password to connect the db in the mongoose.createConnction or mongoose.connect.
  • This user does exist in the db.system.users with readWrite role.
  • The readWrite user only can modify data on all non-system collections the system.js collection

Upvotes: 1

alex10
alex10

Reputation: 2756

Or try to disable authorization or add user that connects to the database.
An example of adding a user to the database:

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

Where:

  • db:"shop" - is name database
  • pwd: "test" - password database
  • user: "admin" - the user through which the connection to the database, example db.auth('admin', 'test')

Upvotes: 0

Amaury Medeiros
Amaury Medeiros

Reputation: 2233

Try giving him the dbAdmin role.

Upvotes: 0

Related Questions