DEV Community

Cover image for The Ultimate MongoDB Command Cheatsheet for Developers
Muhammad ABir
Muhammad ABir

Posted on

The Ultimate MongoDB Command Cheatsheet for Developers

This MongoDB command cheatsheet is the perfect tool for developers who want to work more efficiently and effectively with MongoDB. With 50 of the most commonly used MongoDB commands, this reference guide provides quick and easy access to the information you need to get the job done. Whether you're new to MongoDB or an experienced user, this cheatsheet will help you streamline your development process and maximize your productivity. So, if you want to take your MongoDB skills to the next level, be sure to get this must-have cheatsheet today!

  1. use <database> switch to a specific database
  2. show dbs list all available databases
  3. db.createCollection(<collection_name>) create a new collection
  4. db.<collection_name>.insertOne(<document>) insert a single document into a collection
  5. db.<collection_name>.insertMany([<document1>, <document2>, ...]) insert multiple documents into a collection
  6. db.<collection_name>.find() query all documents in a collection
  7. db.<collection_name>.findOne() query a single document in a collection
  8. db.<collection_name>.find(<filter>) query documents in a collection using a filter
  9. db.<collection_name>.findOne(<filter>) query a single document in a collection using a filter
  10. db.<collection_name>.updateOne(<filter>, <update>) update a single document in a collection
  11. db.<collection_name>.updateMany(<filter>, <update>) update multiple documents in a collection
  12. db.<collection_name>.deleteOne(<filter>) delete a single document from a collection
  13. db.<collection_name>.deleteMany(<filter>) delete multiple documents from a collection
  14. db.<collection_name>.aggregate(<pipeline>) perform aggregation operations on a collection
  15. db.<collection_name>.count() count the number of documents in a collection
  16. db.<collection_name>.drop() delete a collection and its data
  17. db.<collection_name>.createIndex(<field>, <options>) create an index on a field in a collection
  18. db.<collection_name>.ensureIndex(<field>, <options>) ensure an index on a field in a collection
  19. db.<collection_name>.getIndexes() list all indexes for a collection
  20. db.<collection_name>.dropIndex(<index>) delete an index from a collection
  21. db.<collection_name>.dropIndexes() delete all indexes from a collection
  22. db.<collection_name>.reIndex() rebuild all indexes for a collection
  23. db.<collection_name>.explain() provide execution statistics for a query
  24. db.runCommand({<command>}) run a database command
  25. db.getCollectionNames() list all collections in a database
  26. db.getProfilingLevel() get the current profiling level
  27. db.setProfilingLevel(<level>) set the profiling level
  28. db.system.profile.find() query the system.profile collection
  29. db.currentOp() display current in-progress operations
  30. db.killOp(<opid>) kill a specific operation
  31. db.serverStatus() display server status information
  32. db.printCollectionStats() display statistics for a collection
  33. db.repairDatabase() repair a database and its collections
  34. db.cloneDatabase(<source>, <target>) clone a database to a new target database
  35. db.copyDatabase(<source>, <target>) copy a database from a source to a target host
  36. db.shutdownServer() shut down the MongoDB server
  37. db.listDatabases() list all databases on the MongoDB server
  38. db.stats() display database statistics
  39. db.version() display the version of the MongoDB server
  40. db.eval(<function>) evaluate a JavaScript expression on the server
  41. db.loadServerScripts() load scripts stored in the `db.system.js collection
  42. db.auth(<username>, <password>) authenticate as a specific user
  43. db.logout() log out of the current user session
  44. db.getUser(<username>) get information about a specific user
  45. db.createUser(<user>) create a new user
  46. db.updateUser(<username>, <user>) update an existing user
  47. db.dropUser(<username>) delete a user
  48. db.grantRolesToUser(<username>, <roles>) grant roles to a user
  49. db.revokeRolesFromUser(<username>, <roles>) revoke roles from a user
  50. db.getRole(<role>) get information about a specific role.

Top comments (0)