Introduction to MongoDB

Installing MongoDB

Lets get started with installing MongoDB by going to the downloads page (http://mongodb.org/downloads) and following the instructions on the page (also below).

If you are using Windows, you may want to follow the instructions linked here, as opposed to installing MongoDB directly onto your system.

On Mac

I recommend installing Homebrew. Homebrew is a package manager: a software used to install other software. It makes installing MongoDB a breeze.

Open up your Terminal.app (you can find it by typing 'term' in Spotlight) and pasting the code below into your terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then, still in your terminal, type the following:

brew update

And then,

brew install mongodb

That's it!

On Linux

Each Linux distribution has specific instructions to install it. Please refer to the installation instructions for your distribution:

Using MongoDB

Using MongoDB is, you'll be glad to hear, easier than installing it!

If you are using Windows and have installed MongoDB using Docker, as linked above, follow these instructions inside the Kinematic application.

Open up your terminal (Terminal.app on Mac, Terminal on Linux, Console on Windows), unless you've got it already open, and type the following:

mongod

You will not need to do this if you are running the application using Docker.

This starts a MongoDB server in your computer. Now you can start interacting with MongoDB. Do not close the Terminal.app while the mongod process is running.

Open another terminal window, and type the following:

mongo

This starts the interactive console that you can use to interact with the running mongod server.

You can now see the databases that are present in your system (potentially none), by typing:

show dbs

And you can tell mongo that you want to use a specific one (or create a new one) by issuing the command use <db>. In this case, we are going to create a new database called fullstack:

use fullstack

Then, we can see the collections, which hold data, in our database. Because we have just created this database, they will be empty:

show collections

However, we can insert data into a non-existing collection, and it will be created alongside the data. For example, we can create a new student and put it in our database:

db.students.insert({"name": "Jose", "mark": 99})

This creates the collection students, and puts a piece of data inside the collection. The data is {"name": "Jose", "mark": 99}. We can think of this set of key-value pairs (name-Jose and mark-99) as the data representing our first student.

Similarly, we can remove data:

db.students.remove({"name": "Jose"})

Here, MongoDB finds the data that matches the criteria (in this case, our only piece of data), and removes it from the database.

MongoDB is a schema-less database, which means you can have pieces of data with different keys in the same collection.

For example, you could have {"name": "Jose", "mark": 99} and {"item": "chair", "price": 129.5, "store": "IKEA"} in the students collection. This would not make much sense, but you can do it!

Next up, lets look at using MongoDB from within our Python projects!

results matching ""

    No results matching ""