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.
1. MongoDB create database
There’s actually no command to do so – MongoDB automatically creates a database when a collection is created in it.
In Studio 3T:
In the Connection Tree, right-click on the target server. Choose Add Database.
Name your database – and note the info message.
Click on 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 on the target database. Choose Drop Database.
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 this information directly from the Connection Tree.
4. MongoDB show collections
Command:
db.getCollectionNames()
In Studio 3T:
In the Connection Tree, click on your target database.
Click on the Collections folder. This opens up the list of collections within that database.
5. MongoDB create collection
Command:
db.createCollection("collectionName");
In Studio 3T:
Right-click on your target database. Choose Add Collection.
Enter a collection name and configure the settings as needed under the Options, Storage Engine, Validator, and Collation tabs.
Click Create.
6. MongoDB clear collection
Command:
db.users.deleteMany({})
In Studio 3T:
Right-click on your target collection. Choose Clear Collection.
Click on Clear Collection.
7. MongoDB drop collection
Command:
db.collection_name.drop()
In Studio 3T:
Right-click on a target collection. Choose Drop Collection.
Click Drop Collection.
8. MongoDB insert document
Command:
db.collectionName.insert({ field1: “value1”, field2: “value2” })
In Studio 3T:
While in Table View or Tree View, right-click on any cell.
Choose Insert Document. This opens the Insert Document > JSON window.
Enter the key value pairs. There’s no need to create the _id
field – MongoDB does this automatically.
Click on 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 double-click on a value to edit it.
Here’s the complete post on how MongoDB() update works in Studio 3T.
10. MongoDB remove document
Command:
db.collection.deleteOne()
In Studio 3T:
Right-click on your target document. Choose Remove Document.
Click on Remove Document again.
11. MongoDB add field
Command:
{ $set: { <field1>: <value1>, ... } }
In Studio 3T:
Right-click on any cell while in Table View or Tree View. Go to Field > Add Field/Value.
- Enter a field name (e.g. favorite-color)
- Choose the right field type (e.g. String).
- Define the field value (e.g. green).
- Choose where to add the field to (e.g. Current document only).
Click on Add Field/Value.
12. MongoDB rename field
Command:
db.<collection-name>.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 (e.g. All documents in collection).
- Click Rename.
13. MongoDB remove field
Command:
db.<collection-name>.update({}, {$unset: {<field1>:1}}, false, true);
In Studio 3T:
Let’s remove the color
field entirely.
- Right-click a cell with the field you want to delete.
- Choose to remove the field from the current document, documents matching a query, or all documents.
- Click Remove.