Day 21 - The MongoDB Shell

Day 21 - The MongoDB Shell

Before you try to access the shell, make sure that you have MongoDB installed and the Mongo Daemon is running. To install MongoDB go here: To run the Mongo Daemon run this command in terminal: mongod. Then open a new terminal window because the current one has a process running. If you are on linux, it may already be running.


Now that MongoDB is running on your computer, type mongo to enter the Mongo shell. This shell lets you interact with the database using its query language. You can do everything from the shell that you will be able to do with your server application, including make collections and documents, search for documents, update documents, and delete them.

Once the shell is running, type the use command to choose which database you would like to view/modify.


Let's create a collection to keep track of our friends. We'll call the collection friends, and the schema (structure of each document) will look something like this:

// Friend document
  name: String,
  age: Number,
  interests: [String],
  friends: [String]

To insert a new friend document into our friends collection is pretty easy. Just use the db.friends.insert(<PUT FRIEND OBJECT HERE>) command. Here's an example:

  name: 'David',
  age: 20,
  interests: ['Table Tennis', 'School'],
  friends: []


Now, to see all the documents in our friends collection, we just use the find command: db.friends.find(). When find is not passed any arguments, it returns all the documents in the collection.

find all

If we want to find a specific document (or set of documents in the collection, we pass an object to the find command, specifying what document(s) we want): db.friends.find({name: 'David})

find by name

What if David has a birthday? Now we have to update his age to reflect reality. Luckily, mongo has an update function. The first parameter is a query to find the documents we want to update. The second parameter contains the changes we want to make to the document(s). db.friends.update({name: 'David'}, {$set: {age: 21}}). The $set property insures that the document will be updated with the new data, not replaced by it.

update age

You have probably noticed by now that each document is assigned a unique _id by MongoDB when it is created. It's generally best to find and update by _id because then if two users have the same name, they won't both be updated: db.friends.update({_id: ObjectId("5550e1889d1668206d0b5fea")}, {$set: {age: 21}}). The _id will be unique for each document.

update by id

Now it's time to add a few more friends. Here they are:

  name: 'Candice',
  age: 23,
  interests: ['Programming', 'Parker'],
  friends: []

  name: 'Parker',
  age: 23,
  interests: ['Programming', 'Candice'],
  friends: []

  name: 'Sara',
  age: 19,
  interests: ['Cookies', 'Table Tennis', 'Badminton'],
  friends: []

  name: 'Spencer',
  age: 24,
  interests: ['Ultimate', 'Extra Sharp Cheddar Cheese'],
  friends: []

more friends

Well, it's pretty easy to tell that Parker and Candice are friends, so let's tell the database: db.friends.update({name: 'Parker'}, {$set: {friends: [ObjectId("5550e7b49d1668206d0b5feb")}})

db.friends.update({name: 'Candice'}, {$set: {friends: [ObjectId("5550e7b49d1668206d0b5fec")}})

What we did here is add Candice's _id to Parker's friend list and vice versa. This is like foreign keys in Relational Databases. It would be possible to store Candice's document inside Parker's friend array, but that wouldn't make sense because then the data would be duplicated.

What if we want to find friends by interest. I'm going to find all my friends that like table tennis: db.friends.find({interests: 'Table Tennis'}). Notice that even though interests is an array, we only set the value to a string in the query.

find by interest

Finally, let's delete a document. Spencer moved away, so I think it's time for him to leave the friend group: db.friends.remove({name: 'Spencer'}, {justOne: true}). We use the justOne option to make sure we don't delete other friends by accident.


Have fun with MongoDB :)