hey what is up everyone and welcome back to another

video today’s video we continue with the mongodb

crash course so in today’s video we’ll be doing basic

crite operations grad stands for create read update and

delete so doing all of the basic things that most

applications need from their database so we’re just

taking a quick look at that playing around in database

just getting you used to how all of these things work

together and then we continue diving deeper into

individual topics so a concept i want to revisit from the

previous video is how mongodb is structured so the

idea behind a database a collection and a document so

the white part represents the server so on our server we

can have multiple databases and under a database we

get collections so under collections we get documents

so this is the tree of how it works down so if you

understand this concept that we navigate into a

database then into a collection and then we query

whatever documents we’d like in that in that collection

so if you understand that then it will make following

the course really really easy okay so now that you

you’ve seen that little bit of theory i just want to show

you the two ways of starting your mongodb server and

opening the shell so the first one is if you followed my

tutorial we have this bat file that just runs a script it

navigates into the installation directory and then it runs

mongod which is the server and then it runs the shell

so basically this will happen it runs the server and then

it pops up the shell here i can run mongodb commands

just like that okay so that’s the one way if you’re not

doing it like that and you use a different operating

system you just open a terminal after you follow the

installation guide and then you can say sudo mongo d

it’s important to run mongod first but i’m not on mac or

linux so i can just say mongod and then it will start the

server and then in the second terminal you just run

mongo or once again sudo in front if you’re on the

other two just like that and now i’m in the shell with

this terminal just like that okay so i’ll be using the bat

file because it just makes it really quick to open i don’t

really do much but with that out of the way let’s

continue okay so now that we have started the

mongodb server and the shell we can enter our first

command and this is show dbs basically what this

would do it would just list all of the available databases

that is running on your server so we want to create our

own one so databases are created implicitly so you can

create it without it even existing or you can use it

without it even existing it will just create it so for

example we can see there is no database called shop so

if we say use shop we would think it won’t work but it

would just see okay there is no shop and you want to

use a shop so let’s create a shop so if we do that it can

we can see it says switched to db shop so now if i were

to run show dbs again you would see it doesn’t show

up and the reason for this is we did create this database

but there’s no data inside of it yet so it doesn’t show up

here so now we have to create a collection and inside

of this collection we have to add a document so that

there is actual data in this database so let’s create a

collection so in order to create a collection we can first

of all see that there is no collections if i type in show

collections it will show me the collections of the

current database i am switched into so the shop

database currently has no collections so in order to

create one we say db dot and then the collection name

so i’ll give it the name of stock and we can say insert

one just like this and then at the end of this we add a

document that has a json-like syntax so here we can

place in title and we don’t the key value pair we don’t

have to add the quotation marks around the key part we

only have to do it when there’s a string for the value

part so we can say here a book and it has a price so

comma price of 299. so if i were to press enter now we

can see acknowledge true and insert it into this into

the database with this unique id so now if i were to say

show dbs we can see shop shows up here because if

we go into shop and say show collections we can see

there’s a stock collection now so if we want to see what

is inside of the stock collection in the show we just say

db.stock dot find and then we pass it empty empty

parameters and then we press enter and we can see the

data inside so now if you want to print this data out in

a more readable fashion especially when the collection

gets rather large then printing it out in this fashion just

makes it easier easier to read so press up arrow key and

say dot pretty as a function and then it would print it

in this json-like syntax so if you look at this way the

data is stored and to this which is actual json you can

see it is basically the exact same mongodb just uses

bson which stands for binary json so um it’s basically

the same format so if you’ve worked with apis or json

before then understanding how the data looks is pretty

straightforward okay so now that we have seen how to

create a database in a collection let’s see how we can

delete one so if you wanted to delete a database you

can just go into the database by saying use shop for

example and then now we’re inside of the database we

can say db.job database just like this so if you do that it

would say okay shop is dropped and if you would

would you drop a collection you would just say

db.stock dot drop just drop like this and it would drop

the collection but there currently isn’t any collection

because i deleted the database so this is how you would

do these few things okay so in my slides i’m going to

show you the most important crowd operation

commands in mongodb so let’s go over to that okay so

now that we have played around in the database for a

little bit let’s look at some of the most important

commands for creating documents the most important

two commands are insert one and insert many so insert

one allows you to insert one data object at a time and

then it takes in some options separated by a comma

then we have insert many that takes an array of data

objects and also some options we’ll take a look at these

options in more depth when we get to the individual

operation videos then with the read we have find and

find one so this is basically the reading the the

querying of the data so then we have some filter so you

want to find something by some filter we’ll look at

how we can structure these filters in the individual

videos in much more depth and string different queries

together to find the right data and then the options on

both of these would be some type of projection if you

would like to change certain fields to show or not so

find one obviously just returns the first instance where

the filter was found to be true with update we have

three so we have update one which takes in a filter then

the data you want to update and then some options the

menu also takes in a filter and then also some data

which you want to change them with and then some

options then we have replace one and replace one is

basically the data overwrites the update completely so

it doesn’t just add new fields it completely overwrites

so this is if you want to completely override a data

entry or a document and then for delete we have delete

one and delete many delete one by a filter and then

some options and then delete many by some filter so

delete one obviously will only delete one where the

filter is true delete many will delete all of them where

the filter is true okay so first let’s start off by using

insert one for one more time i’ve used it previously

before but let’s quickly create our database so we don’t

have a database and we want to create a database and

say use blogger just like this and we want to create our

user so we can say users dot insert one and then we add

in the user and i said a user looks as follows so it has a

name and this would be me lloyd and it will have a

surname just like this jvr and it will have an email and

this can be gmail.com okay and then we’ll have an

address and this address is going to be another

document so another object and inside of this we have

city and we say new york and inside there we have one

more which is street and this would be wall street and

then we have one last thing so outside of this one um

that is correct we’re still inside here so we say hobbies

and hobbies will be an array of list so let’s say soccer

and let’s say programming just like this so if i would

say enter now we can say acknowledgement is true and

it inserted the with a unique id so you might ask why

am i doing this in the shell why can’t i just type it in

visual studio you can and then just paste it in the

reason i do it in the shell is because this really lets me

think about what i’m doing it makes me feel more

using mongodb from the core okay so let’s insert more

users but this time using insert many so if i were to

clear this and go to show dbs we can see blogger is

there now now you can say show collections to see the

users collection and here we can say find user dot find

dot pretty just like this okay and there we can see the

one so let’s add two more users i’m going to do this by

using insert mini so i’m going to say db.users dot insert

many and this takes an array of documents so we want to insert many so the first one will be name of tywin

and the surname of lannister not even sure if i’m

spelling that correctly and then we’ll have an email of

tywin gmail.com which they probably didn’t live in but

whatever and then we have address and this is once

again a document and we can say city is equal king’s

landing and then street red street just like this then we

can add one more thing which is hobbies and this could

be um horse riding like this i’m not even sure if it’s two

words anyway okay so that is one document so now we

go after this curly brace and add another document so

this would be another user so we can say name jimmy

from our surname and this would be you jimmy you i

don’t know email and gym gmail.com just like this

then we have address which would have city of la and i

don’t know any streets in la so i’m just gonna say street

abc just like this and then the last one is hobbies and

this is once again an array and let’s just say exercise

just like that and now we cross our fingers and hope it

goes through let’s say this again it acknowledged and

we have two entries with their unique ids so now if we

were to go and db.users.find here we can see the users

let’s print it out pretty because that’s just not readable

for me okay and here we have our users user one user

two and user three so we would like to be able to

update something from a specific user so now that

we’ve inserted let’s try and update a username so i’m

just going to clear this and then let’s try updating okay

so now we want to update some of the data so let’s just

quickly look at the data again so what i’m going to try

and update is i’m going to find this user by this id and

i’m going to update its city to something else so let’s do

that i’m going to say db users.update1 and then some

filter with some data the filter will be where the

underscore id is equal to this id so i’m just selecting it

just leaving it like that and then right clicking twice

and then it pastes it in okay and now i want to set

something on this user i said i wanted to change the

address um city so i want to change the address to

whatever i want to then i can say city texas and i

update should put semicolons around texas update i

can see the acknowledgement was true i can find my

users again and now i can see that it updated the

address but it completely removed the street and the

reason for this is because i have reset this it’s the same

as within the array i have totally reset this so if i

wanted to go back so let’s reset this street just like this

and i save and i go back now it would be the same as

previously now i just want to update the new york so i

would go and do the following okay so if i clear that

now i want to update only the city so my data looks

like this okay i only want to make that texas so i’m

going to say the following db users dot update 1 i pass

in the filter and then the data and for the filter i want it i

don’t have its id now i think let’s see i do okay um

where the object id the underscore id is equal to this

object id and then i change by saying dollar sign set

and i want to change and this is where the secret

happens so here i can nest into another document with

the dot notation but this requires the key to have

quotation marks around it so this would be inside of

address so quotation mark address dot city and then

close the quotation marks so this now is only going to

sit inside of the address document the city key it’s

going to change it and we’re going to set this equal to

texas so if i were to save this and run i can see it

acknowledged one and it updated once so let’s search

again and here we can see that it didn’t completely

override the entire document it just changed the city to

texas so that’s how you would update something like

that okay so now that we’ve seen how update 1 works

let’s update many so let’s say we want to add

something new to all of our users that is a new feature

say and let’s say relationship status so i’m going to say

db users dot update mini then i pass in the filter and

then i pass in what i want to update so i want to update

everything all of my user entries so i’m just going to

leave it blank and i want to add a field relationship

status and initially set everyone everyone’s status to

unknown and they can later go and update it

themselves in your application so if i do this and set oh

i forgot to add the underscore set and then closing the

brackets if i update it like this we can see there was

three match counts because i searched for everything

and it modified three things so let’s go check in our

data by finding all our users we can see all of them has

this relationship status unknown field so if one of them

would you go and change it so let’s quickly find them

and let’s change one of them users dot update one i’m

doing update one again just quickly where the

underscore id is equal to this person and we want to

change so dot set the relationship status relation to

single just like this and update now if we close it and

search for our users again we can see these are all

unknown and then this one is changed to single so

that’s how we would update something so let’s quickly

take a look at a few ways to query how we can use the

find in in more advanced ways okay so now we want to

use the find method so first let’s quickly recap by

adding a few posts so i’m going to say db post so if i

show collections we currently only have users so i

want to create a post collection and i’m going to use

insert menu and we want to create posts i’m going to

create so it’s an array of posts i’m going to create let’s

say three posts the first post is going to have a caption

first one it’s going to have a body something cool for

post one and i know this is really repetitive but this is

how you are going to have to get used to working with

data if you’re especially if you’re a back-end engineer

you should learn to work with data fast and in this type

of raw format so okay then we have the author this

lloyd if you want to create a relationship you’ll add a

unique relationship you’ll add the user id i don’t have

that now then we have tags which is an array of strings

that is of hunting and swimming just like this this is

these are the tags and then the next one we have a

caption post 2 we have the body something also

random and then we have author i’m just going to set

this equal to tywin and then we can add the tags and

here i’m going to add swimming rich live just like that

okay and then the last one we can add caption and we

set the sql to post three give it a body just like this and

then we add the last thing which is the tags just like

this okay and here we can add voting yolo something

like that and we add this and we can see it created three

new entries and then we can start finding on them so

the you one you see me used all the time is one which

is just the empty empty parentheses so this would just

find all of the posts so now i want to find a post by say

its tags so i can say db.post.find and pretty this once

again and i can find something in a filter and here i can

say where the tags is equal to swimming so it should

give us these two because those who have swimming

so if you print it out you can see those two were

selected and you can use different type of things on the

property for swimming so let’s say we want to find

everything where the tags are equal to swimming or

there is a tag that exists that is equal to swimming so

this is important to note if you want this checks if there

is a swimming tag that exists in the array it does not

check that that is the only thing in the array if you want

to check that it’s the only thing in the array then you

place array brackets around it like this so now it will

find nothing because none of these has only swimming

okay but let’s remove that and let’s use something

called projection to only display or send back the

necessary fields so i want to find every post where the

tags contain swimming and i only want the author’s

name so here i can say author and set this equal to one

and then then if i press search i see it printed out these

two it gives me the id it always sends you back the id

as well and then the author name so this is something

called projection so i projected it by adding the options

object and inside of the objects i specified author with

a one so zero would be false so if i for instance don’t

want to show this id i can up arrow key and with the

underscore id you should specifically say zero for it to

not show so now if i did and press enter you only see

the author’s names so and then the last i think crowd

operation so we’ll dive deeper into more finding

operations in the individual video but the last thing for

quite operations will be the delete one and delete menu

so if we go back to the users and say db.users.find

look pretty we can see all of these that has relationship

status of unknown i want to delete so that would be a

delete many or for instance if you only want to delete

the first one you find that has unknown it will delete

only the first one it finds to be true so let’s delete one

okay so i’m first going to do something that lets us

delete this entry right over here so i’m going to say

db.users dot delete one i send in some filter and in here

i want where the name is equal to jimmy just like this

sorry jimmy okay there we have deleted it so if i clear

and i search the users i only see two and another neat

thing you can do if you don’t want to see how all the

data you just say count and then it just gives you the

the amount of documents inside of the collection okay

so now if i wanted to delete the users that has an

unknown relationship status so here i’m going to say

dbusers.delete many because i know it’s more than one

and here i can say where relationship status is equal to

unknown just like this boom and you can see

acknowledge true and it deleted too so now if i were to

go back to db users dot find i will see nothing is being

returned because i just deleted everything okay so this

is basically um this for this video i just tried and went

over all of the basic things so you can see how the rest

of this course will be going i would mainly be using

the shell i would giving little scenarios and then i

would just play those queries out inside of the shell

inside of the shell so that you just get the typing

experience and the muscle memory with the learning

of why and how okay so that is the end of the video

today i just have to mention that the most important

things and the in detail work is going to be done in

these sections these crud operations sections where i

focus on each individual aspect of creating reading

updating and deleting data from your mongodb

database so these first few is just to get us to

understand what exactly are we doing and why are we

doing it and just working with terminology and just

playing around a little bit to get the feeling of what is

going where so stick with me through this course and

then hopefully at the end you got a lot out of it but

thanks this is all for the video for today and then i’ll see

you in the next one cheers