Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 218
Sherin Jose Avatar asked Jun 22 '15 07:06

Sherin Jose


People also ask

How do I grant all privileges to a user in MongoDB?

MongoDB: db.grantRolesToUser() method is used to grants an additional role and its privileges to a user. The name of the user to whom to grant roles. An array of additional roles to grant to the user. The level of write concern for the modification.

How do I use findAndModify in MongoDB?

MongoDB – FindAndModify() Method. The findAndModify() method modifies and return a single document that matches the given criteria. By default, this method returns a pre-modification document. To return the document with the modifications made on the update, use the new option and set its value to true.

Which option should be used with findAndModify () command to return the modified document instead of the pre modification document?

Definition. The findAndModify command modifies and returns a single document. By default, the returned document does not include the modifications made on the update. To return the document with the modifications made on the update, use the new option.

How do I drop a role in MongoDB?

MongoDB: db.revokeRolesFromUser() method is used to remove a one or more roles from a user on the current database. The name of the user from whom to revoke roles. The roles to remove from the user. The level of write concern for the modification.


1 Answers

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
like image 200
zangw Avatar answered Oct 21 '22 07:10

zangw