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!
-
use <database>
switch to a specific database -
show dbs
list all available databases -
db.createCollection(<collection_name>)
create a new collection -
db.<collection_name>.insertOne(<document>)
insert a single document into a collection -
db.<collection_name>.insertMany([<document1>, <document2>, ...])
insert multiple documents into a collection -
db.<collection_name>.find()
query all documents in a collection -
db.<collection_name>.findOne()
query a single document in a collection -
db.<collection_name>.find(<filter>)
query documents in a collection using a filter -
db.<collection_name>.findOne(<filter>)
query a single document in a collection using a filter -
db.<collection_name>.updateOne(<filter>, <update>)
update a single document in a collection -
db.<collection_name>.updateMany(<filter>, <update>)
update multiple documents in a collection -
db.<collection_name>.deleteOne(<filter>)
delete a single document from a collection -
db.<collection_name>.deleteMany(<filter>)
delete multiple documents from a collection -
db.<collection_name>.aggregate(<pipeline>)
perform aggregation operations on a collection -
db.<collection_name>.count()
count the number of documents in a collection -
db.<collection_name>.drop()
delete a collection and its data -
db.<collection_name>.createIndex(<field>, <options>)
create an index on a field in a collection -
db.<collection_name>.ensureIndex(<field>, <options>)
ensure an index on a field in a collection -
db.<collection_name>.getIndexes()
list all indexes for a collection -
db.<collection_name>.dropIndex(<index>)
delete an index from a collection -
db.<collection_name>.dropIndexes()
delete all indexes from a collection -
db.<collection_name>.reIndex()
rebuild all indexes for a collection -
db.<collection_name>.explain()
provide execution statistics for a query -
db.runCommand({<command>})
run a database command -
db.getCollectionNames()
list all collections in a database -
db.getProfilingLevel()
get the current profiling level -
db.setProfilingLevel(<level>)
set the profiling level -
db.system.profile.find()
query the system.profile collection -
db.currentOp()
display current in-progress operations -
db.killOp(<opid>)
kill a specific operation -
db.serverStatus()
display server status information -
db.printCollectionStats()
display statistics for a collection -
db.repairDatabase()
repair a database and its collections -
db.cloneDatabase(<source>, <target>)
clone a database to a new target database -
db.copyDatabase(<source>, <target>)
copy a database from a source to a target host -
db.shutdownServer()
shut down the MongoDB server -
db.listDatabases()
list all databases on the MongoDB server -
db.stats()
display database statistics -
db.version()
display the version of the MongoDB server -
db.eval(<function>)
evaluate a JavaScript expression on the server -
db.loadServerScripts()
load scripts stored in the `db.system.js collection -
db.auth(<username>, <password>)
authenticate as a specific user -
db.logout()
log out of the current user session -
db.getUser(<username>)
get information about a specific user -
db.createUser(<user>)
create a new user -
db.updateUser(<username>, <user>)
update an existing user -
db.dropUser(<username>)
delete a user -
db.grantRolesToUser(<username>, <roles>)
grant roles to a user -
db.revokeRolesFromUser(<username>, <roles>)
revoke roles from a user -
db.getRole(<role>)
get information about a specific role.
Top comments (0)