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
List MongoDB databases
Connect to MongoDB database
Exit MongoDB Shell mongosh
Managing MongoDB
Check MongoDB version
Check if MongoDB is running
$ pgrep -fa -- -D | grep mongo
$ pgrep -fa -- -D | grep mongo
$ pgrep -fa -- -D | grep mongo
Restart MongoDB on Linux
db.shutdownServer() $ sudo systemctl start mongodb
db.shutdownServer() $ sudo systemctl start mongodb
db.shutdownServer() $ sudo systemctl start mongodb
Restart MongoDB on Mac OSX
$ brew services stop mongodb-community
$ brew services stop mongodb-community
$ brew services stop mongodb-community
Restart MongoDB on Windows
Winkey + R Type "services.msc" Search "MondoDb" Click "restart"
Winkey + R Type "services.msc" Search "MondoDb" Click "restart"
Winkey + R Type "services.msc" Search "MondoDb" Click "restart"
Change MongoDB user password
db.changeUserPassword(username, NewPassword)
db.changeUserPassword(username, NewPassword)
db.changeUserPassword(username, NewPassword)
Exit from mongosh
Interacting with MongoDB databases
List MongoDB databases
Connect or switch to MongoDB database
Create new MongoDB database
Show name of the current database
Delete MongoDB database
Rename MongoDB database
$ mongodump -d oldDatabaseName path $ mongorestore -d newDatabaseName path/oldDatabaseName.bson $ mongosh use oldDatabaseName db.dropDatabase()
$ mongodump -d oldDatabaseName path $ mongorestore -d newDatabaseName path/oldDatabaseName.bson $ mongosh use oldDatabaseName db.dropDatabase()
$ mongodump -d oldDatabaseName path $ mongorestore -d newDatabaseName path/oldDatabaseName.bson $ mongosh use oldDatabaseName db.dropDatabase()
Interacting with MongoDB collections
List MongoDB collections
Describe MongoDB collection
db.getCollectionInfos( { name: "collectionName" } )db.getCollectionInfos( { name: "collectionName" } )db.getCollectionInfos( { name: "collectionName" } )Describe all collections in MongoDB database
Create MongoDB collection
db.createCollection("collectionName", options)db.createCollection("collectionName", options)db.createCollection("collectionName", options)Delete MongoDB collection
Rename MongoDB collection
db.oldCollectionName.renameCollection("newCollectionName")db.oldCollectionName.renameCollection("newCollectionName")db.oldCollectionName.renameCollection("newCollectionName")Interacting with MongoDB indexes
List MongoDB indexes in a collection
db.collectionName.getIndexes()
db.collectionName.getIndexes()
db.collectionName.getIndexes()
Create a MongoDB index in a collection
db.collectionName.createIndex(keys, options, commitQuorum)
db.collectionName.createIndex(keys, options, commitQuorum)
db.collectionName.createIndex(keys, options, commitQuorum)
Create multiple MongoDB indexes in a collection
db.collectionName.createIndexes( [ keyPatterns ], options, commitQuorum )
db.collectionName.createIndexes( [ keyPatterns ], options, commitQuorum )
db.collectionName.createIndexes( [ keyPatterns ], options, commitQuorum )
Delete MongoDB index
db.collectionName.dropIndex("indexName")db.collectionName.dropIndex("indexName")db.collectionName.dropIndex("indexName")Delete all indexes in MongoDB collection
db.collection.dropIndexes()
db.collection.dropIndexes()
db.collection.dropIndexes()
Rebuild MongoDB indexes
Interacting with MongoDB documents
Count documents in a MongoDB collection
db.collectionName.countDocuments()
db.collectionName.countDocuments()
db.collectionName.countDocuments()
List all documents in a MongoDB collection
db.collectionName.find().toArray()
db.collectionName.find().toArray()
db.collectionName.find().toArray()
Create document in MongoDB
db.collectionName.insertOne( <document>
db.collectionName.insertOne( <document>
db.collectionName.insertOne( <document>
Display a document in MongoDB
db.collectionName.find({ "_id": documentID })db.collectionName.find({ "_id": documentID })db.collectionName.find({ "_id": documentID })Update a document in MongoDB
db.collectionName.updateOne( { "_id": documentID }, <update>db.collectionName.updateOne( { "_id": documentID }, <update>db.collectionName.updateOne( { "_id": documentID }, <update>Delete a document in MongoDB
db.collectionName.deleteOne({ "_id": documentID })db.collectionName.deleteOne({ "_id": documentID })db.collectionName.deleteOne({ "_id": documentID })Replace a document in MongoDB
db.collectionName.replaceOne( { "_id": documentID }, <replacement>db.collectionName.replaceOne( { "_id": documentID }, <replacement>db.collectionName.replaceOne( { "_id": documentID }, <replacement>Backup and restore MongoDB database
Backup MongoDB database
$ mongodump -d oldDatabaseName path
$ mongodump -d oldDatabaseName path
$ mongodump -d oldDatabaseName path
Restore MongoDB database
$ mongorestore -d newDatabaseName path/oldDatabaseName.bson
$ mongorestore -d newDatabaseName path/oldDatabaseName.bson
$ mongorestore -d newDatabaseName path/oldDatabaseName.bson
Managing roles, users and permissions in MongoDB
List MongoDB roles
db.getRoles( { rolesInfo: 1 } )db.getRoles( { rolesInfo: 1 } )db.getRoles( { rolesInfo: 1 } )Create MongoDB user
db.createUser( { user: "userName", pwd: "userPassword", roles:[{role: "roleName" , db:"databaseName"}] } )db.createUser( { user: "userName", pwd: "userPassword", roles:[{role: "roleName" , db:"databaseName"}] } )db.createUser( { user: "userName", pwd: "userPassword", roles:[{role: "roleName" , db:"databaseName"}] } )Delete MongoDB user
Change MongoDB user password
db.changeUserPassword(userName, newPassword)
db.changeUserPassword(userName, newPassword)
db.changeUserPassword(userName, newPassword)
Need to build admin panels or a GUI tool for MongoDB? Check out Forest Admin for MongoDB