DEV Community

Cover image for MongoDB Cheat Sheet
Matthieu Blandineau for Forest Admin

Posted on • Originally published at blog.forestadmin.com

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"
Enter fullscreen mode Exit fullscreen mode

List MongoDB databases

show dbs
Enter fullscreen mode Exit fullscreen mode

Connect to MongoDB database

use databaseName
Enter fullscreen mode Exit fullscreen mode

Exit MongoDB Shell mongosh

quit()
Enter fullscreen mode Exit fullscreen mode

Managing MongoDB

Check MongoDB version

db.version()
Enter fullscreen mode Exit fullscreen mode

Check if MongoDB is running

$ pgrep -fa -- -D | grep mongo
Enter fullscreen mode Exit fullscreen mode

Restart MongoDB on Linux

db.shutdownServer()
$ sudo systemctl start mongodb
Enter fullscreen mode Exit fullscreen mode

Restart MongoDB on Mac OSX

$ brew services stop mongodb-community@yourMongodbVersionNumber
Enter fullscreen mode Exit fullscreen mode

Restart MongoDB on Windows

Winkey + R
Type "services.msc"
Search "MondoDb"
Click "restart"
Enter fullscreen mode Exit fullscreen mode

Change MongoDB user password

db.changeUserPassword(username, NewPassword)
Enter fullscreen mode Exit fullscreen mode

Exit from mongosh

quit()
Enter fullscreen mode Exit fullscreen mode

Interacting with MongoDB databases

List MongoDB databases

show dbs
Enter fullscreen mode Exit fullscreen mode

Connect or switch to MongoDB database

use databaseName
Enter fullscreen mode Exit fullscreen mode

Create new MongoDB database

use newDatabaseName
Enter fullscreen mode Exit fullscreen mode

Show name of the current database

db.getName()
Enter fullscreen mode Exit fullscreen mode

Delete MongoDB database

db.dropDatabase()
Enter fullscreen mode Exit fullscreen mode

Rename MongoDB database

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

Interacting with MongoDB collections

List MongoDB collections

show collections
Enter fullscreen mode Exit fullscreen mode

Describe MongoDB collection

db.getCollectionInfos( { name: "collectionName" } )
Enter fullscreen mode Exit fullscreen mode

Describe all collections in MongoDB database

db.getCollectionInfos()
Enter fullscreen mode Exit fullscreen mode

Create MongoDB collection

db.createCollection("collectionName", options)
Enter fullscreen mode Exit fullscreen mode

Delete MongoDB collection

db.collectionName.drop()
Enter fullscreen mode Exit fullscreen mode

Rename MongoDB collection

db.oldCollectionName.renameCollection("newCollectionName")
Enter fullscreen mode Exit fullscreen mode

Interacting with MongoDB indexes

List MongoDB indexes in a collection

db.collectionName.getIndexes()
Enter fullscreen mode Exit fullscreen mode

Create a MongoDB index in a collection

db.collectionName.createIndex(keys, options, commitQuorum)
Enter fullscreen mode Exit fullscreen mode

Create multiple MongoDB indexes in a collection

db.collectionName.createIndexes( [ keyPatterns ], options, commitQuorum )
Enter fullscreen mode Exit fullscreen mode

Delete MongoDB index

db.collectionName.dropIndex("indexName")
Enter fullscreen mode Exit fullscreen mode

Delete all indexes in MongoDB collection

db.collection.dropIndexes()
Enter fullscreen mode Exit fullscreen mode

Rebuild MongoDB indexes

db.collection.reIndex()
Enter fullscreen mode Exit fullscreen mode

Interacting with MongoDB documents

Count documents in a MongoDB collection

db.collectionName.countDocuments()
Enter fullscreen mode Exit fullscreen mode

List all documents in a MongoDB collection

db.collectionName.find().toArray()
Enter fullscreen mode Exit fullscreen mode

Create document in MongoDB

db.collectionName.insertOne(
   <document>
)
Enter fullscreen mode Exit fullscreen mode

Display a document in MongoDB

db.collectionName.find({ "_id": documentID })
Enter fullscreen mode Exit fullscreen mode

Update a document in MongoDB

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

Delete a document in MongoDB

db.collectionName.deleteOne({ "_id": documentID })
Enter fullscreen mode Exit fullscreen mode

Replace a document in MongoDB

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

Backup and restore MongoDB database

Backup MongoDB database

$ mongodump -d oldDatabaseName path
Enter fullscreen mode Exit fullscreen mode

Restore MongoDB database

$ mongorestore -d newDatabaseName path/oldDatabaseName.bson
Enter fullscreen mode Exit fullscreen mode

Managing roles, users and permissions in MongoDB

List MongoDB roles

db.getRoles(
    {
      rolesInfo: 1
    }
)
Enter fullscreen mode Exit fullscreen mode

Create MongoDB user

db.createUser(
    {
        user: "userName",

        pwd: "userPassword",

        roles:[{role: "roleName" , db:"databaseName"}]
    }
)
Enter fullscreen mode Exit fullscreen mode

Delete MongoDB user

db.dropUser(userName)
Enter fullscreen mode Exit fullscreen mode

Change MongoDB user password

db.changeUserPassword(userName, newPassword)
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)