
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to create MongoDB user on existing database with correct role?
To create a new user in MongoDB, use createUser(). Following is the query −
> db.createUser( ... { ... user: "John", ... pwd: "123456", ... roles: [ { role: "readWrite", db: "test" } ], ... mechanisms: [ "SCRAM-SHA-256" ] ... } ... )
This will produce the following output −
Successfully added user: { "user" : "John", "roles" : [ { "role" : "readWrite", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-256" ] }
Following is the query to show all users −
> db.getUsers();
This will produce the following output −
[ { "_id" : "test.John", "user" : "John", "db" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-256" ] } ]
- Related Articles
- Create a new user and set role in MongoDB
- How to change the password in MongoDB for existing user?
- How to create a user in MongoDB v3?
- How to create a new database in MongoDB?
- How to create a database in MongoDB using Java?
- Create User SQL in SAP HANA database
- Check user rights before attempting to CREATE MySQL DATABASE?
- How can we create user accounts in MySQL database server?
- Working with MongoDB $concatArrays in $project on existing multi-array field
- How to create MySQL user with limited privileges?
- How to create a "user rating" scorecard with CSS?
- How can we create a table from an existing MySQL table in the database?
- How to create a database on command line in MySQL?
- How to create an index with MongoDB?
- End User Database

Advertisements