Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Blog
    • Community
    • Testimonials
    • Whitepapers
    • Reports
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Careers
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • Download
  • My 3T
search

Studio 3T Knowledge Base

  • Documentation
  • Tutorials
  • Workshops
Take the fastest route to learning MongoDB. Cover the basics in two hours with MongoDB 101, no registration required.
Start the free course

13 Most Common MongoDB Commands

Posted on: 22/01/2019 (last updated: 31/08/2021) by Kathryn Vargas

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.

Right-click on the target server and choose Add Database

Name your database – and note the info message.

Name your newly created MongoDB database

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.

Right click on the target database and 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.

View the full list of databases directly from the Connection Tree

Skip the MongoDB commands, download Studio 3T, and be productive from Day One.

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.

Click on the target database then the Collections folder to show all collections.

5. MongoDB create collection

Command: 

db.createCollection("collectionName");

In Studio 3T:

Right-click on your target database. Choose Add Collection.

Right click on a database and choose Add Collection

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 right settings

Click Create.

6. MongoDB clear collection

Command: 

db.users.deleteMany({})

In Studio 3T: 

Right-click on your target collection. Choose Clear Collection.

Choose your target collection and click on 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.

Right click on a target collection and 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.

Insert JSON document to be added

Click on Validate JSON to make sure the syntax is correct.

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.

Double click to edit date in place.

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.

Choose your target document and click Remove document

Click on Remove Document again.

To delete all documents from a collection, follow the instructions on how to clear a collection.

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).
Configure the settings of your new field as needed

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.

  1. Right-click any cell that contains the field you want to rename.
  2. Go to Field > Rename Field.
  3. Update the field name.
  4. Choose where the field name should be updated (e.g. All documents in collection).
  5. Click Rename.
Rename a MongoDB field in your current document, all documents, or documents matching your query criteria

13. MongoDB remove field

Command: 

db.<collection-name>.update({}, {$unset: {<field1>:1}}, false, true);

In Studio 3T:

Let’s remove the color field entirely.

  1. Right-click a cell with the field you want to delete.
  2. Choose to remove the field from the current document, documents matching a query, or all documents.
  3. Click Remove.
Choose to remove a field from the current document, documents matching a query, or all documents

Want more MongoDB hacks?

  • Don’t know the MongoDB query language? Build queries visually by dragging and dropping fields through the Visual Query Builder
  • Autocomplete field names, collection names, and more with IntelliShell, Studio 3T’s built-in mongo shell
  • Avoid data mix-ups by color-coding your databases and collections
  • View multiple MongoDB collections at once to quickly compare data

How helpful was this article?
This article was hideous
This article was bad
This article was ok
This article was good
This article was great
Thank you for your feedback!

About The Author

Kathryn Vargas

When she's not writing about working with MongoDB, Kathryn spends her free time exploring Berlin's food scene, playing the drums, learning languages (current mission: German), and hiking.

Article navigation

Related articles

  • 3T Software Labs acquires Robomongo, the most widely used MongoDB tool
  • MongoDB Tutorial: Learn MongoDB in 2 Hours
  • How MongoDB Indexes Work (With Examples)
  • MongoDB, A Database with Porpoise – When You Have a Whale of a Performance Problem
  • MongoDB find Method: Introduction & Examples

Studio 3T

MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

Education

  • Free MongoDB Tutorials
  • Connect to MongoDB
  • Connect to MongoDB Atlas
  • Import Data to MongoDB
  • Export MongoDB Data
  • Build Aggregation Queries
  • Query MongoDB with SQL
  • Migrate from SQL to MongoDB

Resources

  • Feedback and Support
  • Sales Support
  • Knowledge Base
  • FAQ
  • Reports
  • White Papers
  • Testimonials
  • Discounts

Company

  • About Us
  • Blog
  • Careers
  • Legal
  • Press
  • Privacy Policy
  • EULA

© 2023 3T Software Labs Ltd. All rights reserved.

  • Privacy Policy
  • Cookie settings
  • Impressum

We value your privacy

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data.

By clicking “Accept all”, you consent to the storage of cookies and the processing of personal data for these purposes, including any transfers to third countries. By clicking on “Decline all”, you do not give your consent and we will only store cookies that are necessary for our website. You can customize the cookies we store on your device or change your selection at any time - thus also revoking your consent with effect for the future - under “Manage Cookies”, or “Cookie Settings” at the bottom of the page. You can find further information in our Privacy Policy.
Accept all
Decline all
Manage cookies
✕

Privacy Preference Center

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data. Please choose for which purposes you wish to give us your consent and store your preferences by clicking on “Accept selected”. You can find further information in our Privacy Policy.

Accept all cookies

Manage consent preferences

Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

Google Analytics

Google Ads

Bing Ads

Facebook

LinkedIn

Quora

Hotjar

Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

HubSpot

Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

Accept selected