1. User management:
To connect to a database:
Switch to the admin database:
To create an administrator account:
1234567 |
db.createUser( { user: "rootUser" , pwd : "rootPass" , roles: [ { role: "root" , db: "admin" } ] } ) |
Log in to the database:
12 |
> use admin > db.auth( "adminUser" , "adminPass" ) |
> Displays all accounts in the current database:
1 |
db.system. users . find ().pretty() |
Switch to the specified database:
Show users of the current database:
To create a user:
1234567 |
db.createUser( { user: "testUser" , pwd : "testPass" , roles: [ { role: "readWrite" , db: "testdb" }] } ) |
To delete a specified user:
1 |
> db.dropUser( "testUser" ) |
To exit the database connection:
2. Database management:
Switch to the database you want to create:
To create a user:
1234567 |
db.createUser( { user: "testUser" , pwd : "testPass" , roles: [ { role: "readWrite" , db: "testdb" }] } ) |
Sign in with your new account:
1 |
> db.auth( "testUser" , "testPass" ) |
Creates a new collection in the current database;
1 |
> db.table1.save({ "id" : "1" }) |
Displays the collection of the current database:
1 |
> db.table1.save({ "id" : "1" }) |
Inserts a single piece of data into the current collection:
1 |
> db.table1.insert({ "id" : "3" }) |
Displays all data content in the current collection:
Displays the data content specified in the current collection:
1 |
> db.table1.findOne({ "id" : "3" }) |
Displays the database currently in use:
Delete the current database:
MongoDB User and Database administration commands