MongoDB User and Database administration commands

Source: Internet
Author: User

1. User management:

To connect to a database:

1 mongo 127.0.0.1:27017

Switch to the admin database:

1 > use admin

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:

1 > use testDB

Show users of the current database:

1 > show users

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:

1 > quit()

2. Database management:

Switch to the database you want to create:

1 > use testdb;

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:

1 > db.table1.find()

Displays the data content specified in the current collection:

1 > db.table1.findOne({"id":"3"})

Displays the database currently in use:

1 > db()

Delete the current database:

1 > db.dropDatabase()

MongoDB User and Database administration commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.