So now that everything is set up and you should have a working mongodb server and the shell
connected to it,
let me clear this screen by running cls and this only well clears the screen, it doesn't do anything else
and now let's work with the database,
let's get started working.
First of all, you can type show dbs to see existing databases and you do start off with three different
databases that store some metadata,
we don't need to worry about these.
Now first of all, let's connect to a database with the use command and you can connect to a brand new
database by simply typing its name
even if this doesn't exist yet, it will be created on the fly once you start inserting data. Now in there,
you can create a new collection also on the fly because no collection exists right now and you do this
by typing
db, this refers to the database you are currently connected to,
so shop in my case and then the name of the collection where you want to insert data, let's say products
and this also doesn't need to exist yet.
And then you can insert one new product with the insertOne command and you pass this json object
to it,
so this double curly braces
surround the data you want to insert into this collection
and there you can add your keys, your key value fields, like name Max.
Now the good thing is here, this would work but you can omit the quotation marks around the key name
here in the shell, not around the value because this should be some text and text is marked by quotation
marks but around the key name you can omit it,
this is simply a little feature,
it will be stored with the quotation marks behind the scenes.
Now we can add a second value by adding a comma and then the age, 29 and now we will insert a document
with the name and the age.
However for a product, this doesn't make too much sense,
so let's maybe name it a book and then give it a price of 12.99.
So if I now hit enter,
you should get this command which basically confirms that the data was inserted into the database and
that mongodb automatically generated a unique ID for it which it returns you here.
Now let's have a look at our data then right,
this can be done by simply running db products,
so again the currently connected database and then the collection we stored the data in
and then by typing find. This gives us if you add no other arguments, which we'll do in the course but
not right now,
if you add no other arguments this will give you all the data in that collection, find.
So this gives us the one entry we added
and here you see the quotation marks around the key names too.
Now there also is another version number of this, you can add .pretty at the end to simply output it in
a prettier way,
it's still the same data, just easier to read.
Now try that on your own, pause the video,
try that on your own and add another product on your own,
although try adding name and price and also one extra field, like the description with some dummy text. Were you
successful?
Well you would repeat db products insertOne
and by the way, if you repeat a command which you executed before, you can also just press the up key
on your keyboard to cycle through your past commands,
this allows you to quickly go back to a past command, simply edit it a bit and re-execute it,
this will not change the past data,
it will be treated as a new command but it's easier for you,
you don't have to write everything again.
So here you could add a T-shirt with some price
and as I said, now let's also add a new key, a description where I
say this is a high quality T-shirt or whatever you want. If I now hit enter,
this will be inserted as well
and now let me clear the screen again with the cls command
and let me execute find
pretty again
and now we see two documents in that collection
and you also already see that schemaless thing, our documents don't have exactly the same structure.
Now let's take it one step further and let's insert another document
by the way you can press alt and then left arrow to jump through these words quicker
and let's name this computer, ramp up the price a little bit
and then simply type a high quality computer because I'm really creative when it comes to text and there,
let's add some details and this now will be an embedded document with another pair of curly braces inside
of that other document
and here, we could have something like CPU
and that is let's say the Intel i7,
this one.
No I'm not getting paid,
I don't know if that even exists but I think it does and memory, let's say there we have 32 gigabytes
like this.
And now if I insert this and if find them again, now we see all three
and you also see that embedded document.
So this is just playing around with the shell here,
of course we can also delete and update documents and we can filter when we're retrieving them,
we don't have to always retrieve all of them but that's just our first real quick start.
This is how we will work with mongodb for a big chunk of this course
and now let me show you how this relates to the real world where you would of course write an application
with PHP, with node, with C++ and would interact with the database from there.
So how does that shell stuff you're learning in this course relate to that?