MongoDB Basic Command Line Cheat Sheet

Introduction

If you’re just getting started with MongoDB or it’s been a while since you’ve used Mongo, you might need a reference for the most common MongoDB commands. This article will give a list of the most common tasks and the MongoDB command used to do execute it. Print it out, keep it on your desk, memorize it, we encourage you to do whatever you need to do get value out of this cheat sheet. Now after all it is a cheat sheet, so let’s get straight into it.

The Cheat Sheet

Start your MongoDB Daemon. You need Mongo running before you can run the Mongo Shell or interact with Mongo in any way.

mongod

Start the MongoDB Shell. If you want to interact with Mongo through the command line this is how you’ll do it.

mongo

List all MongoDB databases. This is a good command to sanity check to just make sure all your databases are still there.

show dbs

Create a database. Know that the database won’t actually be created until you insert a document until a collection.

use db

Switch to another database. Using the Mongo Shell will default to using the test database so you’ll need to switch to your database or create your own.

use otherdb

List all collections in database. Another good sanity check command to make sure your collections exist and are named exactly like you as you intended.

show collections

Create a collection.

db.createCollection("demoCollection")

Insert a new document. You’ll run commands very similar to this all the time. Get used to it’s key-value pair syntax.

db.demoCollection.insert({"field1": "value1", "field1": "value2"})

Get all documents in a collection. Super common command but if you have too many documents you’ll need to limit with conditions.

db.demoCollection.find()

To get documents in a more readable format add .pretty() to the .find(). Add this on to all your find commands, as it’s basically unreadable without it.

db.demoCollection.find().pretty()

Drop/delete a collection. Use this command and all drop/deletion commands with care.

db.demoCollection.drop()

Show users.

show users

Update a single document. Get familiar with $set and $push to update your documents.

db.demoCollection.update(
{ field1: "value1" },
{ $set:
{
    field2: "anotherValue"
}
}
)

Update multiple documents. If you’re updating multiple documents at once through the shell use this or consider interacting with mongo through a higher-level language like python or javascript.

db.demoCollection.update(
{ field1: "value1" },
{ $set: field2: "anotherValue" },
{
    multi: true
}
)

Conclusion

We reviewed a few of the basic commands including creating a database, creating a collection, and inserting a new document plus more. We hope this reference is useful to you as you learn MongoDB or as a reminder of some of the basic commands for MongoDB in the command line.

As we alluded to in the last command, if you find that you’re interacting with mongo through the console frequently you should consider using Python, Javascript, or another programming language to automate your tasks. Working with the console is essential sometimes and is highly regarded by developers, but you want to automate as much as possible and that is better done through these other languages.

Thanks again for reading and don’t hesitate to reach out with any questions.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.