search
HomeDatabaseMongoDBHow to create a user in MongoDB v3?

如何在 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" } ]
   }
);

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" } ]
...    }
... );

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

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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
MongoDB: Addressing Concerns and Addressing Potential IssuesMongoDB: Addressing Concerns and Addressing Potential IssuesApr 28, 2025 am 12:19 AM

Common problems with MongoDB include data consistency, query performance, and security. The solutions are: 1) Use write and read attention mechanisms to ensure data consistency; 2) Optimize query performance through indexing, aggregation pipelines and sharding; 3) Use encryption, authentication and audit measures to improve security.

Choosing Between MongoDB and Oracle: Use Cases and ConsiderationsChoosing Between MongoDB and Oracle: Use Cases and ConsiderationsApr 26, 2025 am 12:28 AM

MongoDB is suitable for processing large-scale, unstructured data, and Oracle is suitable for scenarios that require strict data consistency and complex queries. 1.MongoDB provides flexibility and scalability, suitable for variable data structures. 2. Oracle provides strong transaction support and data consistency, suitable for enterprise-level applications. Data structure, scalability and performance requirements need to be considered when choosing.

MongoDB's Future: The State of the DatabaseMongoDB's Future: The State of the DatabaseApr 25, 2025 am 12:21 AM

MongoDB's future is full of possibilities: 1. The development of cloud-native databases, 2. The fields of artificial intelligence and big data are focused, 3. The improvement of security and compliance. MongoDB continues to advance and make breakthroughs in technological innovation, market position and future development direction.

MongoDB and the NoSQL RevolutionMongoDB and the NoSQL RevolutionApr 24, 2025 am 12:07 AM

MongoDB is a document-based NoSQL database designed to provide high-performance, scalable and flexible data storage solutions. 1) It uses BSON format to store data, which is suitable for processing semi-structured or unstructured data. 2) Realize horizontal expansion through sharding technology and support complex queries and data processing. 3) Pay attention to index optimization, data modeling and performance monitoring when using it to give full play to its advantages.

Understanding MongoDB's Status: Addressing ConcernsUnderstanding MongoDB's Status: Addressing ConcernsApr 23, 2025 am 12:13 AM

MongoDB is suitable for project needs, but it needs to be used optimized. 1) Performance: Optimize indexing strategies and use sharding technology. 2) Security: Enable authentication and data encryption. 3) Scalability: Use replica sets and sharding technologies.

MongoDB vs. Oracle: Choosing the Right Database for Your NeedsMongoDB vs. Oracle: Choosing the Right Database for Your NeedsApr 22, 2025 am 12:10 AM

MongoDB is suitable for unstructured data and high scalability requirements, while Oracle is suitable for scenarios that require strict data consistency. 1.MongoDB flexibly stores data in different structures, suitable for social media and the Internet of Things. 2. Oracle structured data model ensures data integrity and is suitable for financial transactions. 3.MongoDB scales horizontally through shards, and Oracle scales vertically through RAC. 4.MongoDB has low maintenance costs, while Oracle has high maintenance costs but is fully supported.

MongoDB: Document-Oriented Data for Modern ApplicationsMongoDB: Document-Oriented Data for Modern ApplicationsApr 21, 2025 am 12:07 AM

MongoDB has changed the way of development with its flexible documentation model and high-performance storage engine. Its advantages include: 1. Patternless design, allowing fast iteration; 2. The document model supports nesting and arrays, enhancing data structure flexibility; 3. The automatic sharding function supports horizontal expansion, suitable for large-scale data processing.

MongoDB vs. Oracle: The Pros and Cons of EachMongoDB vs. Oracle: The Pros and Cons of EachApr 20, 2025 am 12:13 AM

MongoDB is suitable for projects that iterate and process large-scale unstructured data quickly, while Oracle is suitable for enterprise-level applications that require high reliability and complex transaction processing. MongoDB is known for its flexible document storage and efficient read and write operations, suitable for modern web applications and big data analysis; Oracle is known for its strong data management capabilities and SQL support, and is widely used in industries such as finance and telecommunications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.