So now, it's finally time
to get started writing some MongoDB code
or running some MongoDB queries.
And as you'll see,
that will be quite different to what we did
in the SQL world.
When it came to working with SQL and MySQL,
we started by setting up a database and then some tables,
and we started by setting up the structure of our table.
Now, here with MongoDB, it will be quite different.
If we wanna insert some data, if we wanna store some data,
we don't need to set up any table at all,
simply because, with MongoDB,
or with NoSQL databases in general, as you learned,
the idea of having a fixed structure
with predefined data types just doesn't exist.
Instead, as you learned,
the idea is that you have these collections
with documents inside of them,
and that you can have different documents
with different structures in one of the same collection.
Now, in reality, you will typically end up
with similarly structured documents in the same collection,
but you simply don't have a fixed predefined structure
for your documents in a collection.
So therefore, with MongoDB,
we don't start by setting up tables or collections,
or by defining some structures,
we can just start by inserting some data.
As a first step though,
we have to select a database
into which we wanna select data.
And we ran the show dbs command
to see which databases we have.
But these three databases,
which are listed here out of the box for me,
are all built-in databases that store some system data
in some configuration,
and we therefore don't wanna use them
for our application data.
Instead, what we wanna do here
is we wanna use a new database.
And for this, we can run the use command here.
Simply type use,
and then the database name you wanna work with.
And as an example for this section,
I'll come back to an example I used
in the SQL introduction section,
where we had Restaurants and Reviews,
simply because there I showed you how you would store
that kind of data with SQL.
Now, I think it's great
to understand how we would store that with NoSQL.
But again, we'll not start with the full example here.
We'll come to that later in this section,
instead I wanna start with the basic CRUD operations.
And hence, we'll just have a look
at some dummy Restaurants data that we might wanna store
in a Restaurants collection.
So first of all, we need a database as mentioned,
and we can use the use command for this,
and then a fitting database name.
So not a collection name for the restaurants,
but the name for a database
that might hold multiple collections related
to restaurants and reviews.
It could be ratingportal,
because maybe we're building a portal for users
to leave ratings.
So now you can hit Enter, and that's all.
As you can tell,
we now switched to this new database here.
And of course, that wasn't the database we had before,
but by simply using it, it was created for us.
You still don't see it if you run show dbs,
because it will actually only really be created
once you start inserting some data into a collection
in that database.
But switching to it is the first step for doing so.
Now once you've switched,
you can start inserting data.
Because as I mentioned,
you don't need to create a table or collection to do so,
instead it will be created on the fly
if you simply insert data.
Now to insert data into a collection,
we write db to refer to the currently active database.
And then, we need to tell MongoDB
into which collection we wanna insert data.
For example, the restaurant's collection.
So you just type the restaurants collection
after a dot after db.
Now, this collection doesn't exist yet,
but just like the database, it will be created on the fly
once we insert data.
And as a side note,
this already looks a lot like JavaScript.
db.restaurants, that looks
like we always access properties on JavaScript objects
throughout this course, with this dot notation.
And indeed, there is a lot of similarity.
The MongoDB query language,
which we are using here,
is based on JavaScript, you could say,
but it's really important to understand
that this is a specific language written
by the MongoDB team, or created by the MongoDB team,
understood by the MongoDB server
that really can only be used with MongoDB.
Because that's a key difference compared
to SQL-based database systems.
Whereas SQL is a generally standardized language
that works with all SQL-based database systems,
NoSQL database systems
typically bring their own query language.
So this language here works with MongoDB only in the end,
but the documentation
of the different database engines is really good in the end
and it's all straightforward
to pick up those query languages.
So here, with that, we access a collection
on the database we are currently using,
and then we can type another dot and now execute a bunch
of methods, you could say, to run queries
against this collection in this database.
For example, the insertOne method.
And again, this looks just like JavaScript.
We're now executing this method on this collection.
And as the name suggests,
insertOne can be used to insert data,
one piece of data, to be precise, into this collection.
There also is a insertMany command
in case you wanna bulk insert data,
and insert multiple documents at once.
But we're going to get started in a simple way here
and just insert one document.
And then, a document just looks like a JavaScript object
with opening and closing curly braces.
And between those curly braces,
you can now have your key value peers.
For example, for the restaurant, we can have a name.
So we can add such a name field here.
You can come up with any keys you want.
And that name here could be a string created
with double quotes, for example, "Munich Schnitzelhouse".
And here, we already see our first data type,
which you can use:
a string created with quotes, just as in JavaScript.
There also are the other data types you would expect,
like numbers, or also dates, or also nested data types,
like erase or nested objects.
Because as I mentioned before in this section already,
that's a specialty of NoSQL database systems,
that you can have nested structured data
instead of splitting your normalized flat data
across multiple tables, as you would do it with SQL.
So having nested data would be totally normal here.
And indeed, we can already add our first piece
of nested data here.
Besides adding the name, we could add an address field here,
because every restaurant should also have an address,
and that now could indeed be some structured document.
So another pair of curly braces,
where we store the address for this specific restaurant
as a sub-document, you could say,
with more key value fields.
For example, here, we could have the street,
and that could be "Some Street 5".
And then, separated by a comma,
here we could also then have the street number,
and that could be an actual number 25,
or to support things like 23b,
we could also make it a string again.
Now here, I automatically have a line, a break,
because I ran out of space in that first line,
but it's still one command here.
And with that, we could insert such a restaurant.
Now, we'll come back to the question
of whether it's a good idea to store the address
as a nested object instead of a standalone document
in a standalone collection,
but for the moment that's the kind
of restaurant I wanna store.
And we can now hit Enter to send this command
to the MongoDB server to which we're connected.
And as a response,
we get this kind of answer here,
where we get the ID of the inserted document,
because MongoDB will automatically create a unique ID
for every document that's inserted.
So that's kind of similar to SQL-based databases,
where we also had unique IDs.
We also have them here.
They just look different.
In the end, they are long, unique strings generated
by the MongoDB server.
And with that, our data is stored.
Now let's add a second piece of data.
And for this, I'll just quickly duplicate that command
from before and switch the data, "Another Street".
I just noticed I had a street number here in my first piece
of data already.
It doesn't matter too much.
It's just an example.
Of course, it should have been stored
in this separate street number.
So I've fixed this here in the second document,
where I didn't add an extra number in the street here.
And then, we can also go back here and change the name
to "Burger House".
I'm really creative here, as you can tell.
And hit Enter to insert this as well.