As you get started with MongoDB, it will take some time to learn how to query, so we’ve compiled a list of the most common MongoDB commands and how they’re written.
As an alternative, we also show you how to run them – without typing a single line of code – using Studio 3T as a GUI. Even better than a cheat sheet!
1. MongoDB create database
There’s actually no command in the MongoDB shell to do so – MongoDB automatically creates a database when you store data in a collection or document.
In Studio 3T:
In the Connection Tree, right-click the target server. Choose Add Database.
![Right-click on the target server and choose Add Database to create a database in MongoDB](https://studio3t.com/wp-content/uploads/2019/01/mongodb-create-database-studio3t75.png)
Name your database – and note the info message.
![Name your newly created MongoDB database](https://studio3t.com/wp-content/uploads/2019/01/mongodb-create-database-name-1.png)
Click OK. Make sure to add a collection!
2. MongoDB drop database
Command:
db.dropDatabase() //drops the current database you're in
In Studio 3T:
Let’s drop (or delete) the newly created database, manatee
.
Right-click the target database. Choose Drop Database.
![Right click on the target database and choose Drop database to delete the database from MongoDB](https://studio3t.com/wp-content/uploads/2019/01/mongodb-drop-database-2.png)
Click Drop Database.
3. MongoDB show databases
Command:
db.adminCommand( { listDatabases: 1, nameOnly: true} ) // lists database names only
In Studio 3T:
No clicks needed here. You can view all databases from the Connection Tree.
![View the full list of databases on your MongoDB server in the Connection Tree, no need to type MongoDB commands](https://studio3t.com/wp-content/uploads/2019/01/mongodb-show-databases-list75.png)
4. MongoDB show collections
Command:
db.getCollectionNames()
In Studio 3T:
In the Connection Tree, click the target database.
Click the Collections folder. This opens up the list of collections within that database.
![Click on the target database then the Collections folder to show all collections in a MongoDB database.](https://studio3t.com/wp-content/uploads/2019/01/mongodb-show-collections-expand.png)
Simply double-click the collections to show data in MongoDB. Learn more about viewing collection data.
5. MongoDB create collection
Command:
db.createCollection("collectionName");
In Studio 3T:
Right-click the target database. Choose Add Collection.
![Right click on a database and choose Add Collection to create a collection in MongoDB](https://studio3t.com/wp-content/uploads/2019/01/mongodb-create-collection-1.png)
Enter a collection name and configure the settings as needed under the Options, Storage Engine, Validator, and Collation tabs.
![Enter a collection name and configure the settings for your new MongoDB collection in a GUI instead of learning complicated command syntax](https://studio3t.com/wp-content/uploads/2019/01/mongodb-create-collection-add.png)
Click Create.
Learn more about working with MongoDB collections in Studio 3T.
6. MongoDB clear collection
Command:
db.collection.deleteMany({})
In Studio 3T:
Right-click the target collection. Choose Clear Collection.
![Choose your target collection and click Clear Collection to run the equivalent MongoDB deleteMany command.](https://studio3t.com/wp-content/uploads/2019/01/mongodb-clear-collection-deletemany75.jpg)
Click Clear Collection.
7. MongoDB drop collection
Command:
db.collection.drop()
In Studio 3T:
Right-click the target collection. Choose Drop Collection.
![Right click on a target collection and choose Drop collection to remove a collection from a MongoDB database](https://studio3t.com/wp-content/uploads/2019/01/mongodb-drop-collection-60.png)
Click Drop Collection.
8. MongoDB insert document
Command:
db.collection.insertOne({ field1: “value1”, field2: “value2” })
In Studio 3T:
While in Table View or Tree View, right-click any cell.
Choose Document > Insert Document. This opens the Insert JSON Document dialog.
Enter the key value pairs. There’s no need to create the _id
field – MongoDB does this automatically.
![Insert documents into a MongoDB collection using the Insert JSON document dialog.](https://studio3t.com/wp-content/uploads/2019/01/mongodb-insert-JSON-document-1024x434.png)
Click Add Document if adding one document, or Add & Continue if adding more documents.
9. MongoDB update document
Command:
db.collection.updateOne(<filter>, <update>, <options>)
In Studio 3T:
Studio 3T supports in-place editing, so all you need to do is double-click a value to edit it.
![Double-click a value to edit data in MongoDB.](https://studio3t.com/wp-content/uploads/2019/01/mongodb-update-document-value.gif)
Here’s the complete article on how the MongoDB updateOne () method works in Studio 3T.
10. MongoDB delete document
Command:
db.collection.deleteOne()
In Studio 3T:
Right-click the target document. Choose Delete Document.
![Choose the target document and click Delete Document to remove or delete a document from a MongoDB collection.](https://studio3t.com/wp-content/uploads/2019/01/mongodb-delete-document-1024x438.jpg)
11. MongoDB add field
Command:
{ $set: { <field1>: <value1>, ... } }
In Studio 3T:
Right-click anywhere in Tree View. Go to Field > Add Field/Value.
- Enter a field name
- Choose the field type
- Define the field value
- Choose where to add the field (Current document only, Documents matching query criteria, All documents in collection).
![Configure the settings of your new field in your MongoDB documents as needed](https://studio3t.com/wp-content/uploads/2019/01/add-field-mongodb-document.png)
Click Add Field/Value.
12. MongoDB rename field
Command:
db.collection.update({query}, {\$rename: { <field1> : <newName1> , <field2> : <newName2>}})
In Studio 3T:
Let’s rename the field from favorite-color
to color
.
- Right-click any cell that contains the field you want to rename.
- Go to Field > Rename Field.
- Update the field name.
- Choose where the field name should be updated (Current document only, Documents matching query criteria, All documents in collection).
- Click Rename.
![Rename a MongoDB field in your current document, all documents, or documents matching query criteria](https://studio3t.com/wp-content/uploads/2019/01/mongodb-rename-field-1.png)
13. MongoDB remove field
Command:
db.collection.update({}, {$unset: {<field1>:1}}, false, true);
In Studio 3T:
Let’s remove the color
field entirely.
- Right-click the cell of the field you want to delete.
- Choose where you want to remove the field (Remove from current document only, Remove from all documents, Remove from documents matching current query).
- Click OK.
![Choose to remove a field from the current document, documents matching a query, or all documents in the MongoDB collection](https://studio3t.com/wp-content/uploads/2019/01/mongodb-remove-field-all-documents-1.png)
![](https://studio3t.com/wp-content/uploads/2024/12/Blog-CTA-2-1024x536.gif)
This article was originally published by Kathryn Vargas and has since been updated.