Basic MongoDB Commands

Feature thumb course thumb

Below is a cheat sheet for working with the Mongo database.

Create a database

In order to switch to a database, you can use the command (if the database was not already created, this will automatically generate it):

use mydbname

 

Creating a database user

Here is how you can create a new authorized user for a local admin database:

Show All Databases

To show all of the databases with data inside of them, you can run the command:

show dbs

 

Create a Collection

First switch into the database, and run the command:

db.createCollection('books')

 

If this works it will return:

{ "ok" : 1 }

 

View Collections Inside of a Database

Run the command:

show collections

 

Add / Insert into the Collection

To insert a single new items into a collection, run the command:

 

Add / Insert Multiple Items Into the Collection

To insert new items into a collection, run the command:

 

Query All Documents in a Collection

Run the command:

db.books.find().pretty()

 

Query for a Record in a Collection

Assuming that we have a book named "Skill Up", run the command:

db.books.find( {name: "Skill Up"} )

 

This is comparable to running the SQL query:

SELECT * from books WHERE name = "Skill Up"

 

Query for a a Portion of a Nested Array Element in a Document Using $slice

Assuming that we have a book named "OOP Programming" with multiple authors, run the following commands to see the difference and how the $slice function works with nested array elements:

 

Query for a Record in a Collection with Projections (Specific Fields)

Assuming that we have a book named "Confident Ruby", run the command:

This is comparable to running the SQL query:

SELECT _id, publishedDate, authors from books WHERE name = "Confident Ruby"

 

How to Exclude the _id Field from a find Query

Assuming that we have a book named "Confident Ruby", run the command:

This is comparable to running the SQL query:

SELECT publishedDate, authors from books WHERE name = "Confident Ruby"

 

How to Specify Which Fields Not to Return in a find Query

Assuming that we have a book named "Confident Ruby", run the command:

This is comparable to running the SQL query:

SELECT _id, publishedDate, name from books WHERE name = "Confident Ruby"

 

How to Include Nested Fields in a find Query

Assuming that we have a book named "Confident Ruby", run the command:

 

And you can always add a call to the pretty() function if you want to have a formatted version of the query.

It's important to note that the find function will return all items in the database that match the query. This is important because if you run the query and have code that expects a single value, it will run into an error because you'll need to iterate over a collection of documents.

 

Return a Single Document from a Query

In order to fix this issue, if you need to be assured that you will only receive a single document you can run the command:

If you run the findOne command, please note that it can lead to some confusing query results, especially if you query for an item with multiple potential results.

 

How to Query for Documents that Have Fields or Are Missing Values

If we create a new document that stores a book object with the following code, we can run queries to check to see which books have reviews and which ones do not:

 

How to Query for a Portion of a String

There are many times where you will not know the full value of a key, in that case you can leverage regular expressions to search for a document: