We’ve been working on many projects lately that have utilized MongoDB as the primary means of database storage. I have previous experience building and using MySQL databases, so the idea of these NoSQL databases is a new concept for me.
I’m not one to shy away from new technologies, so I’ve been trying to embrace MongoDB and learn how to use it.
One of the most important things I’ve been learning is how to view the databases, collections, and records that I’ve saved in my various applications through the MongoDB command line interface.
Let’s do a quick walk through and pretend I have a database dedicated to baseball.
Once you have Mongo installed on your machine, you run the interface by typing mongo in your terminal. Now, you can bring up a list of databases by typing show databases.
How do we use a particular database? Easy! Just type use [database_name]
Awesome! Of course, you’ll want to do more than just “use” the database. We want to see what’s inside it. This is accomplished by telling mongo to show us all collections (e.g., think of these as “tables” in a traditional SQL database).
Awesome! Now we have a collection of teams and collection of players. Well, let’s display everything within a particular collection. In this case, let’s print out all teams that we have stored in our database.
Now, let’s say you’re looking for a particular record. How do you limit your search to just one thing? Like this:
Now, you can imagine that if we had more data, there are a lot more things that we could search for and find. It’s pretty powerful!
Anyway, this was a quick tutorial on how to use the Mongo DB CLI. I hope you found it helpful!