MongoDB basic Commands

Source: Internet
Author: User

Mongodb maintain basic commands

user admin commands apply to 2.* version 3.* some changes  

1. View detailed shard information

Mongos>sh.status ()

---sharding Status---

Sharding version: {

"_id": 1,

"Version": 4,

"Mincompatibleversion": 4,

"CurrentVersion": 5,

"Clusterid": ObjectId ("569c8e920d9b1cb4bbf4169a")

}

Shards:

{"_id": "Shard1", "host": "Shard1/192.168.0.167:20200,192.168.0. 25:20,100 "}

{"_id": "Shard2", "host": "shard2/192.168.0. 167:20020,192.168.0. 167:20,030 "}

{"_id": "Shard3", "host": "shard3/192.168.0. 167:30003,192.168.0. 25:30,001 "}

Databases

{"_id": "admin", "partitioned": false, "PRIMARY": "Config"}

{"_id": "Test", "partitioned": false, "PRIMARY": "Shard3"}

2. Show only Shards

>useconfig;

>db.shards.find ()

{"_id": "Shard1", "host": "Shard1/10.10.10.39:20100,10.10.10.49:20200"}

{"_id": "Shard2", "host": "Shard2/10.10.10.39:20010,10.10.10.49:20020"}

{"_id": "Shard3", "host": "Shard3/10.10.10.39:30001,10.10.10.49:30002"}

3. View cluster-related information

Mongos>db.serverstatus ()

4. Database user and database information

Mongos> Show DBS;

Admin 0.016GB

Config 0.016GB

TestDB 0.156GB

mongos> use ttdb \ \ CREATE DATABASE

Switched to DB Ttdb

Mongos> db.adduser (' TT ', ' 123456 ') \ \ Add user name and password for database

Warning:the ' addUser ' Shell helper isdeprecated. Please use ' createUser ' instead

Successfully added User: {"user": "tt", "Roles": ["Dbowner"]}

#/mnt1/data/mongodbtest/mongodb/bin/mongo 192.168.0.25:20001/ttdb

MongoDB Shell version:2.6.5

Connecting To:192.168.0.25:20001/ttdb

Mongos> Show collections \ \ did not log in to the database using the user password, reported no permissions error

2016-01-18t17:42:27.981+0800 Error: {

"$err": "Not authorized for query on Ttdb.system.namespaces",

"Code": 13

} at src/mongo/shell/query.js:131

mongos> db \ \ View the database currently in use

Ttdb

Mongos> Db.auth (' TT ', ' 123456 ')

1 \1 for verification Success

Mongos>show Collections

Mongos> Use Ttdb

Switched to DB Ttdb

Mongos> Show Collections

Mongos> db.dropdatabase ();    \\ Deleting a database

{"Dropped": "Ttdb", "OK": 1}

mongos> Use admin

Switched to DB admin

Mongos> Show DBS;

Admin 0.016GB

Config 0.016GB

mongos> Use admin

mongos> db.system.users.find () \ \ View the current database user

Mongos> Use Ttdb

Switched to DB Ttdb

Mongos> db.dropuser ("sa") \ \ \ Delete database user

True

>use Ttdb

>db.table1.stats (); \ \ View the status of the table

Db.adduser ("TX", "TX", ture); \\ to add a read-only database user

2016-01-26t16:16:26.153+0800referenceerror:ture is not defined

Mongos> db.adduser ("TX", "TX", True);

Warning:the ' addUser ' Shell helper isdeprecated. Please use ' createUser ' instead

Successfully added User: {"user": "TX", "Roles": ["read"]}

Mongos>db.createcollection ("account")

{

"OK": 0,

"ErrMsg": "Not authorized in Ttdb to execute command {create:\" account\ "}",

"Code": 13

} \ \ Create a collection will error

5. Balancer (copy related)

mongos> use Config

Switched to DB Config

Mongos> sh.getbalancerstate (); \ \ View process status

True

Mongos> Sh.stopbalancer ();

Waiting for active hosts ...

Waiting for the balancer lock ...

Waiting again for active hosts afterbalancer are off ...

Mongos> sh.getbalancerstate ();

False

Mongos> Sh.startbalancer ();

Mongos> sh.getbalancerstate ();

True


MongoDB basic 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.