MongoDB
MongoDB Cursor Tutorial: Learn with EXAMPLE
What is Cursor in MongoDB? When the db.collection.find () function is used to search for documents in...
Creating a user administrator in MongoDB is done by using the createUser method. The following example shows how this can be done.
db.createUser( { user: "Guru99", pwd: "password", roles:[{role: "userAdminAnyDatabase" , db:"admin"}]})
Code Explanation:
If the command is executed successfully, the following Output will be shown:
Output:
The output shows that a user called "Guru99" was created and that user has privileges over all the databases in MongoDB.
To create a user who will manage a single database, we can use the same command as mentioned above but we need to use the "userAdmin" option only.
The following example shows how this can be done;
db.createUser( { user: "Employeeadmin", pwd: "password", roles:[{role: "userAdmin" , db:"Employee"}]})
Code Explanation:
If the command is executed successfully, the following Output will be shown:
Output:
The output shows that a user called "Employeeadmin" was created and that user has privileges only on the "Employee" database.
First understand the roles which you need to define. There is a whole list of role available in MongoDB. For example, there is a the "read role" which only allows read only access to databases and then there is the "readwrite" role which provides read and write access to the database , which means that the user can issue the insert, delete and update commands on collections in that database.
db.createUser( { user: "Mohan", pwd: "password", roles:[ { role: "read" , db:"Marketing"}, role: "readWrite" , db:"Sales"} } ] })
The above code snippet shows that a user called Mohan is created, and he is assigned multiple roles in multiple databases. In the above example, he is given read only permission to the "Marketing" database and readWrite permission to the "Sales" database.
What is Cursor in MongoDB? When the db.collection.find () function is used to search for documents in...
Regular expressions are used for pattern matching, which is basically for findings strings within...
$20.20 $9.99 for today 4.6 (115 ratings) Key Highlights of MongoDB Tutorial PDF 107+ pages eBook...
What is Query Modifications? Mongo DB provides query modifiers such as the 'limit' and 'Orders' clause...
You do not need install the MongoDB server and configure it. You can deploy MongoDB Atlas server...
What is Primary Key in MongoDB? In MongoDB, _id field as the primary key for the collection so...