DEV Community

Cover image for MongoDB Cheat Sheet Part 1: Basics
Matt Ryan
Matt Ryan

Posted on

MongoDB Cheat Sheet Part 1: Basics

Start MongoDB

sudo systemctl start mongod
Enter fullscreen mode Exit fullscreen mode

Stop MongoDB

sudo systemctl stop mongod
Enter fullscreen mode Exit fullscreen mode

Restart MongoDB

sudo systemctl restart mongod
Enter fullscreen mode Exit fullscreen mode

MongoDB Status

sudo systemctl status mongod
Enter fullscreen mode Exit fullscreen mode

Enter Mongo Shell

mongo
Enter fullscreen mode Exit fullscreen mode

Connect to remote host

mongo --host <hostname or ip address> --port <port>
Enter fullscreen mode Exit fullscreen mode

Example:
Alt Text

Connect to a specific database

mongo <host>/<database>
Enter fullscreen mode Exit fullscreen mode

Example:
Alt Text

Log Into MongoDB

mongo -u <username> -p <password> --authenticationDatabase <dbname>
Enter fullscreen mode Exit fullscreen mode

Example:
Alt Text

Create, select or switch database

use <database name>
Enter fullscreen mode Exit fullscreen mode

Example:
Alt Text

Show current database

db
Enter fullscreen mode Exit fullscreen mode

Show all databases

show dbs
Enter fullscreen mode Exit fullscreen mode

Show all users on current database

show users
Enter fullscreen mode Exit fullscreen mode

Show all roles on current database

show roles
Enter fullscreen mode Exit fullscreen mode

Further Reading

Top comments (0)