MongoDB Cheat Sheet

We gathered a list of handy MongoDB and mongosh commands and queries when working with MongoDB databases. Please ping us @ForestAdmin if you’d like to add something to the cheat sheet!

This is the second post from our Cheat Sheets series following the PostgreSQL one.

Getting started with MongoDB

Install MongoDB Shell mongosh

Login and enter MongoDB Shell mongosh

$ mongosh
//OR
$ mongosh "mongodb://localhost:27017"

List MongoDB databases

show dbs

Connect to MongoDB database

use databaseName

Exit MongoDB Shell mongosh

quit()

Managing MongoDB

Check MongoDB version

db.version()

Check if MongoDB is running

$ pgrep -fa -- -D | grep mongo

Restart MongoDB on Linux

db.shutdownServer()
$ sudo systemctl start mongodb

Restart MongoDB on Mac OSX

$ brew services stop mongodb-community@yourMongodbVersionNumber

Restart MongoDB on Windows

Winkey + R
Type "services.msc"
Search "MondoDb"
Click "restart"

Change MongoDB user password

db.changeUserPassword(username, NewPassword)

Exit from mongosh

quit()

Interacting with MongoDB databases

List MongoDB databases

show dbs

Connect or switch to MongoDB database

use databaseName

Create new MongoDB database

use newDatabaseName

Show name of the current database

db.getName()

Delete MongoDB database

db.dropDatabase()

Rename MongoDB database

$ mongodump -d oldDatabaseName path
$ mongorestore -d newDatabaseName path/oldDatabaseName.bson
$ mongosh
use oldDatabaseName
db.dropDatabase()

Interacting with MongoDB collections

List MongoDB collections

show collections

Describe MongoDB collection

db.getCollectionInfos( { name: "collectionName" } )

Describe all collections in MongoDB database

db.getCollectionInfos()

Create MongoDB collection

db.createCollection("collectionName", options)

Delete MongoDB collection

db.collectionName.drop()

Rename MongoDB collection

db.oldCollectionName.renameCollection("newCollectionName")

Interacting with MongoDB indexes

List MongoDB indexes in a collection

db.collectionName.getIndexes()

Create a MongoDB index in a collection

db.collectionName.createIndex(keys, options, commitQuorum)

Create multiple MongoDB indexes in a collection

db.collectionName.createIndexes( [ keyPatterns ], options, commitQuorum )

Delete MongoDB index

db.collectionName.dropIndex("indexName")

Delete all indexes in MongoDB collection

db.collection.dropIndexes()

Rebuild MongoDB indexes

db.collection.reIndex()

Interacting with MongoDB documents

Count documents in a MongoDB collection

db.collectionName.countDocuments()

List all documents in a MongoDB collection

db.collectionName.find().toArray()

Create document in MongoDB

db.collectionName.insertOne(
   <document>
)

Display a document in MongoDB

db.collectionName.find({ "_id": documentID })

Update a document in MongoDB

db.collectionName.updateOne(
	{ "_id": documentID },
	<update>
)

Delete a document in MongoDB

db.collectionName.deleteOne({ "_id": documentID })

Replace a document in MongoDB

db.collectionName.replaceOne(
	{ "_id": documentID },
	<replacement>
)

Backup and restore MongoDB database

Backup MongoDB database

$ mongodump -d oldDatabaseName path

Restore MongoDB database

$ mongorestore -d newDatabaseName path/oldDatabaseName.bson

Managing roles, users and permissions in MongoDB

List MongoDB roles

db.getRoles(
    {
      rolesInfo: 1
    }
)

Create MongoDB user

db.createUser(
	{
		user: "userName",

		pwd: "userPassword",

		roles:[{role: "roleName" , db:"databaseName"}]
	}
)

Delete MongoDB user

db.dropUser(userName)

Change MongoDB user password

db.changeUserPassword(userName, newPassword)

Need to build admin panels or a GUI tool for MongoDB? Check out Forest Admin for MongoDB