Skip to content

kevbite/mongodb-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

MongoDB Tutorial

This tutorial is meant to be a beginners guide to using MongoDB.

Get Started

The fastest way to connect to a MongoDB instance is to use Docker.

Installing Docker Desktop

Follow the follow installation guides to get Docker Desktop installed locally:

Docker Desktop

MongoDB Shell

Running the MongoDB Shell

To connect to a database instance within MongoDB you can use the MongoDB shell. We can execute this program via docker with the offical mongo docker image on Docker Hub.

We can run the MongoDB shell without connecting to a database just to play around, to do this we execute mongosh with the --nodb argument.

docker run -it mongo:latest mongosh --nodb

Current Mongosh Log ID: 645381eb4b8b50a86c12f7f8
Using Mongosh:          1.6.1
>

Playing with MongoDB Shell

The mongo shell is an interactive JavaScript interface to MongoDB. That means we can run any arbitrary JavaScript! This makes the shell very powerful as we can write complex scripts to interact with MongoDB without any other tooling.

Let's try writing some basic JavaScript, we'll start by printing "Hello World".

> print('Hello World')
Hello World

Now let's do some basic calculations:

> 1 + 2 * 3
7

Let's try to use the Maths functions inside JavaScript, these will work as well:

> Math.pow(10, 1000);
Infinity

We can also declare functions that we can reuse, below is Fizz Buzz

> const fizzBuzz = input => {
  if (input % 15 === 0) return 'FizzBuzz';
   if (input % 3 === 0) return 'Fizz';
   if (input % 5 === 0) return 'Buzz';
   return input;
 };
> fizzBuzz(2)
2
> fizzBuzz(30)
FizzBuzz

We can also use arrays which comes in handy when receiving lists of documents from the database.

> [0, 1, 2, 3, 4, 5].map(fizzBuzz).join(', ');
FizzBuzz, 1, 2, Fizz, 4, Buzz

Feel free to try out more JavaScript commands inside the MongoDB Shell.

Database Interaction

To do the next section we'll need to start up a local MongoDB instance, this again can be done via docker with the offical mongo image.

docker run -p 27017:27017 mongo:latest

Then we'll need to connect to it via the MongoDB shell, this time instead of passing in --nodb we can pass in a connection string which points to the docker host (your local machine).

docker run -it --network=host mongo:latest mongosh "mongodb://localhost/"

To interact with the database, the shell gives us a few global variable helpers. The main one we'll be using is db. This is how we'll interact with the current database we're connected to.

Database

Let's start by just executing db:

> db
test

This will return the database that we're connected to. We can swap the database with another shell helper called use, if you've used any other database this might be familiar.

> use my-test
switched to db my-test
> db
my-test

Collections

Within a database you'll find collections, collections are somewhat similar to tables within a relational database, so you might have a collection to store vehicles and another one to store drivers.

These collections are accessed via the db global object, everything is created dynamically when required within MongoDB so we are not required to upfront declare a collection, we can just use it.

So let's just query a vehicles and a drivers collection within the test database.

// change to test database
> use test
switched to db test

// query our vehicles and drivers
> db.vehicles.find()
> db.drivers.find()

You'll notice that there is no error because there is no collection, this is one of MongoDB benefits of how easy it is to get going without any complex setup! But as you may have notice nothing is returned, this is because we've got nothing in these collections yet!

Help!

Within MongoDB the easiest way to find out what you can do on a certain object is to execute the help() method on it. Try executing help() on our db and our vehicles collections and see what you see!

> db.help()

> db.vehicles.help()

Also, MongoDB has one of the best documentation sites out there with pretty much all the information you'll ever need. We'll refer back to the documentation in some of these sections but feel free to take a browse: The MongoDB Manual

Be a Developer

We're going to go though some basic developer operations that you might need to execute while running MongoDB.

Inserting data

To insert data in to MongoDB we can execute 2 functions on our collection insertOne() and insertMany().

To insert a single document in to our vehicles collection call the insertOne function passing in an object:

> db.vehicles.insertOne({type: 'Car', year: 2018, make: 'Honda', model: 'Civic', registrationNumber: 'CU57ABC', colour: 'red'})
{
  "acknowledged" : true,
  "insertedId" : ObjectId("5d17d3d455fa784abd28f481")
}

As you'll notice we get a insertedId return this is the server generated _id to reference our document because we didn't specify one, If we specify one it will use it.

> db.vehicles.insertOne({ _id: ObjectId("5d17d5ef55fa784abd28f482"), type: 'Car', year: 2018, make: 'Honda', model: 'Civic', registrationNumber: 'CU57ABC', colour: 'red' })
{
  "acknowledged" : true,
  "insertedId" : ObjectId("5d17d5ef55fa784abd28f482")
}

We can also insert documents that have completely different properties, for our vehicles collection let's insert in some bicycles!

> db.vehicles.insertOne({ type: 'Bicycle', bicycleType: 'Road Bike', year: 2019, make: 'Giant', model: 'Contend 1', colour: 'red' })
{
  "acknowledged" : true,
  "insertedId" : ObjectId("5d17d70a55fa784abd28f483")
}

This can be very beneficial when building applications that are required to store data of different structures.

We can also insert many documents at once using the insertMany() function, let's just insert a bunch of vehicles in to our database:

> db.vehicles.insertMany([
   { type: 'Car', year: 2018, make: 'Honda', model: 'Civic', registrationNumber: 'CE57ABC', colour: 'blue' },
   { type: 'Car', year: 2018, make: 'Honda', model: 'Civic', registrationNumber: 'EU57ABC', colour: 'blue' },
   { type: 'Car', year: 2019, make: 'Honda', model: 'Jazz', registrationNumber: 'BD57ABC', colour: 'white' },
   { type: 'Car', year: 2019, make: 'Peugeot', model: '208', registrationNumber: 'BD57DEF', colour: 'yellow' },
   { type: 'Car', year: 2019, make: 'Renault', model: 'Clio', registrationNumber: 'TT57DEF', colour: 'red' },
   { type: 'Bicycle', bicycleType: 'Road Bike', year: 2019, make: 'Liv', model: 'Avail 3', colour: 'blue' },
   { type: 'Bicycle', bicycleType: 'Road Bike', year: 2019, make: 'Specialized', model: 'Allex E5', colour: 'red' },
   { type: 'Bicycle', bicycleType: 'Mountain Bike', year: 2015, make: 'Specialized', model: 'Rockhopper Expert', colour: 'sliver' }
 ]);
{
 "acknowledged" : true,
 "insertedIds" : [
   ObjectId("5d17dac755fa784abd28f484"),
   ObjectId("5d17dac755fa784abd28f485"),
   ObjectId("5d17dac755fa784abd28f486"),
   ObjectId("5d17dac755fa784abd28f487"),
   ObjectId("5d17dac755fa784abd28f488"),
   ObjectId("5d17dac755fa784abd28f489"),
   ObjectId("5d17dac755fa784abd28f48a"),
   ObjectId("5d17dac755fa784abd28f48b")
 ]
}

Querying data

To query data inside MongoDB we can call the find function on the collection and pass in a query. The query is a way to declare what we want to find. Let's start by finding our vehicle that we inserted in the previous section with the _id of ObjectId("5d17d5ef55fa784abd28f482").

We'll pass in the an object with an _id set to our id to find:

> db.vehicles.find({ _id: ObjectId("5d17d5ef55fa784abd28f482") })
{ "_id" : ObjectId("5d17d5ef55fa784abd28f482"), "type" : "Car", "make" : "Honda", "model" : "Civic", "registrationNumber" : "CU57ABC", "colour" : "red" }

You may also want to create a separate variables in JavaScript that contain your query so you can use them again and again:

> var query = { _id: ObjectId("5d17d5ef55fa784abd28f482") };
> db.vehicles.find(query);
{ "_id" : ObjectId("5d17d5ef55fa784abd28f482"), "type" : "Car", "make" : "Honda", "model" : "Civic", "registrationNumber" : "CU57ABC", "colour" : "red" }

You can make the documents returned easier to read by adding a call to pretty() on the end of your find method:

db.vehicles.find(query).pretty();

The above query is using the short hand for the $eq Operator, this can alternatively be written like the follow query:

> var query = { _id: { $eq: ObjectId("5d17d5ef55fa784abd28f482") } };

MongoDB has a vast number of query operators which allows you to filter documents in many different ways, the full list of the operators can be found on their documentation.

Let's now change this around to query for all bicycles:

var query = { type: 'Bicycle' };

We may also want to use a less than operator to only return bicycles that are less than the year of 2019.

var query = { type: 'Bicycle', year: { $lt: 2019 } };

Then we can also extend our query to return all vehicles that are of the colour of yellow as well. We can achieve this by using the or operator:

var query = {
  $or: [
    { type: 'Bicycle', year: { $lt: 2019 }},
    { colour: 'yellow' }
  ]
};

Now try build up some of your own queries to query our vehicles collection.

Updating data

Updating data is similar to query it, we'll use a function called update on the collection.

We first use the same queries used in finding the data but then we specify an update object on how we want to update the document. These update objects are built up of update operators.

Let's change the colour of our vehicle with the _id of ObjectId("5d17d5ef55fa784abd28f482") to 'orange'.

> var query = { _id: ObjectId("5d17d5ef55fa784abd28f482") };
> var update = { $set: { 'colour': 'orange' } };
>
> db.vehicles.updateOne(query, update);
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
>
> db.vehicles.find(query).pretty()
{
        "_id" : ObjectId("5d17d5ef55fa784abd28f482"),
        "type" : "Car",
        "make" : "Honda",
        "model" : "Civic",
        "registrationNumber" : "CU57ABC",
        "colour" : "orange"
}

Perfect, now we've got an orange car!

Feel free to make up some of your own updates to the documents.

Querying parkrun Data

Connect to the database

We'll need to drop out the current shell and create a new session to a database hosted in MongoDB Atlas. Atlas is a SaaS offering of MongoDB provided by MongoDB.

To connect to the database run the following command but replace the <username> and <password> with the given details.

docker run -it mongo:latest mongosh "mongodb+srv://cluster0.zt6noj2.mongodb.net/" --username <username>
Enter password: <password>

Once connected run the following to see the structure of the parkrun data:

> db.parkruns.findOne();

Now use the data inside mongodb with the knowledge you've got so far to query the parkrun dataset to find answers to the following questions.

Questions

  1. What is the name of the parkrun with the _id of ObjectId("5c2a324534656f06e4bd21c6")?

  2. What is the location (lat/lon coordinates) of Polokwane parkrun in South Africa?

  3. What parkrun name starts with the letter Ö?

  4. Where outside of the UK has a parkrun with the name starting with Vall?

  5. Which 2 parkruns have had attendance of over 995?

  6. Which parkrun was cancelled due to a Soap Box Race?

  7. How many parkruns have showers that people can use?

  8. How many countries have parkruns?

  9. Which country has the most amount of parkruns?

  10. Which country has the least amount of parkruns?

  11. Which country has the fastest average seconds ran for the whole country?

  12. What is total events run for the whole of Italy?

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published