MongoDB Basic Command Line Cheat Sheet

Have a Database Problem? Speak with an Expert for Free
Get Started >>

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.

1
mongod

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

1
mongo

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

1
show dbs

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

1
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.

1
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.

1
show collections

Create a collection.

1
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.

1
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.

1
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.

1
db.demoCollection.find().pretty()

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

1
db.demoCollection.drop()

Show users.

1
show users

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

1
2
3
4
5
6
7
8
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.

1
2
3
4
5
6
7
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.