Home > Database > MongoDB > body text

How to create a user in MongoDB v3?

PHPz
Release: 2023-09-13 11:05:02
forward
707 people have browsed it

如何在 MongoDB v3 中创建用户?

To create a user in MongoDB v3, use the createUser() method. This allows you to create a user and upon creation also add the user, password and role. These roles assign permissions. The syntax is as follows -

use admin
db.createUser(
   {
      user: “yourUserName",
      pwd: "yourPassword",
      roles: [ { role: "yourPermission", db: "yourDatabase" } ]
   }
);
Copy after login

Let us implement the above syntax to create a user in MongoDB v3 -

> use admin
switched to db admin
> db.createUser(
...    {
...       user: "Robert",
...       pwd: "robert",
...       roles: [ { role: "readWrite", db: "sample" } ]
...    }
... );
Copy after login

Above, we have set the following for the new user -

User: Robert
Password: robert
Roles: readWrite
This will produce the following output:
Successfully added user: {
   "user" : "Robert",
   "roles" : [
      {
         "role" : "readWrite",
         "db" : "sample"
      }
   ]
}
Copy after login

us A new user "Robert" has been successfully created above.

The above is the detailed content of How to create a user in MongoDB v3?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!