mongo

 

 


Check Version

 

mongod --version

mongo -version

 


Connection

 

# Local

mongo --port 28015

# Remote

mongo --host mongodb0.example.com:28015

Authentication

# If you specify --password without the user’s password, the shell will prompt for the password.

mongo --username alice --password --authenticationDatabase admin --host mongodb0.examples.com --port 28015

 


Working with the mongo Shell

 

# To display the database you are using, type db:

db

# use anther db after connect

use <database>

db.stats()

output

{
        "db" : "MyDB",
        "collections" : 16,
        "views" : 0,
        "objects" : 562434,
        "avgObjSize" : 904.5233894110243,
        "dataSize" : 508734708,
        "storageSize" : 146960384,
        "numExtents" : 0,
        "indexes" : 17,
        "indexSize" : 8900608,
        "fsUsedSize" : 76155506688,
        "fsTotalSize" : 107004026880,
        "ok" : 1
}

 


Authentication Database

 

When adding a user, you create the user in a specific database.

This database is the authentication database for the user.

The user’s name and authentication database serve as a unique identifier for that user.

[1] That is, if two users have the same name but are created in different databases, they are two separate users.

If you intend to have a single user with permissions on multiple databases,

create a single user with roles in the applicable databases instead of creating the user multiple times in different databases.