Mongodb Commands Cheat Sheet

  



MongoDB Cheat Sheet Db Info Commands Start a mongo shell to remote Db Commands Users Create User Backup / Restore Backup Restore with mongorestore Find Querying References Db Info Commands Start a mongo shell to remote Db mongo -host Test01 -port -username adminuser -password -authenticationDatabase admin Commands use db. As part of our MongoDB Guide, we’ve compiled this cheat sheet of common and not-so-common MongoDB commands. (This article is part of our MongoDB Guide. Use the right-hand menu to navigate.) Table of Contents. Pretty Print Create Collection Create Indexes Create index Create sparse index Create compound index Create geo index Create partial index. In this post, you will learn about MongoDB commands, which could get you started and perform minimum database related activities such as create, update, and drop a collection (table).

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.

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

show dbs

Mongodb Commands List

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

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.

Create a collection.

db.createCollection('demoCollection')

Mongodb Commands Cheat Sheet Excel

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'})

Download full version photoshop for mac. Get all documents in a collection. Super common command but if you have too many documents you’ll need to limit with conditions.

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.

Show users.

Mongodb
show users

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

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

In this post, you will learn about MongoDB commands which could get you started and perform minimum database related activities such as create, update, drop a collection (table). These commands are ideally meant for MongoDB beginners and could be taken as the cheat sheet. You may want to bookmark this page for quick reference.

MongoDB Commands Cheatsheet

The following is the list of the commands:

    • Start and stop the MongoDB Database
    • Access the MongoDB database using Shell
    • Show all databases
    • Create a database, say, testdb; Switch to the database

      Until a collection is created in a database, the database name is not listed as a result of execution of the command, “show dbs”

    • Add a collection
    • Show all collections in a database; Execute the “use dbname” command to access the database before executing the command given below.

      Dvd x player for mac. The following command also work: Batman arkham city for mac.

    • Insert a record in the collection; A record is inserted in the collection, “user”.
    • Display list of records of a collection; “user” collection is used.
    • Display a list of records matching with value (s) of specific fields
  • Drop the collection
  • Create users in the database; The below command creates a user with username as “ajitesh” and having the role such as “readWrite” and “dbAdmin”
  • Show users; If executed without selecting a database, it displays all users along with database information.
  • Login into the database with username and password

    For user created in above command, the login command would look like the following:

Mongodb Commands Cheat Sheet

References

Summary

In this post, you learned about the MongoDB cheatsheet commands (especially, helpful for beginners) which could help you quickly get started with MongoDB.

Did you find this article useful? Do you have any questions or suggestions about this article? Leave a comment and ask your questions and I shall do my best to address your queries.

Cheat
  • First Principles Understanding based on Physics - April 13, 2021
  • Precision & Recall Explained using Covid-19 Example - April 11, 2021
  • Moving Average Method for Time-series forecasting - April 4, 2021