With those basics about the data, the documents and how we work with data out of the way,
let's shift our focus towards the crud operations and how we work with the different crud operations
we may use in mongodb.
Now first of all, what is crud all about,
why does this matter?
You can use mongodb for a broad variety of things, you might be building an application or you
have some data which is shipped to you as one file and you just want to run some analytics on it or you're
an administrator of a database, of a mongo database,
in all these cases, you will have to interact with the database.
Now in the application case, you might have an app, a mobile app or a website where user interacts
with your code in the end
and that code can be written in nodejs, php, can be a desktop app written in C++,
really no limits there and you will have your mongodb driver included in your application then. In
the other cases like for analytics,
you might be using the business intelligence connector mongodb offers or you directly use the shell
or some other import mechanism provided by your favorite BI tool or as an administrator, you'll work
with the shell like we're doing it right now. In all these cases, you want to interact with your mongodb
server and for an application like a website,
you typically want to be able to create elements, you want to be able to read, update and delete them.
Let's say you were building a blog,
you want to be able to create new posts, to deliver those posts to your users,
you want to be able to update these posts,
so to change them or maybe even delete them. With analytics, you also at least want to be able to read
the data,
you might never change it but you want to read it
and as an admin, you might want to do all these things to fix some data or to play around with it as
we are doing it.
So these are these core crud operations
and in the end, these operations are the only thing you do with your data,
this is what you have your data for, to create it, manage it, read it,
all these things and you do these things with the mongodb server
and there of course, in the end on a collection and the documents in that collection. Now for these crud operations,
we have a couple of important queries you should keep in mind and you'll also find in the official docs
of course in case you want to check out all the details or dive a bit deeper.
There is insertOne which we already saw,
insertOne allows us to insert some data into a collection and all these methods I'm showing you are
directly executed on a collection as we did it in the last lectures
and we can also pass some options along with insertOne,
we'll not to worry about that right now,
we'll dive into that later though.
Besides inserting one document, we can also insert many documents by the way,
so if you have a use case where you need to insert more than one document at a time, you can do that
too,
this is important for creating data. Now for reading data,
we got find and find also allows us to pass in some arguments.
Right now we're calling it without anything, just find empty parentheses,
now you can pass in a filter and filters are very powerful,
they allow you to narrow down the data you're retrieving, something you of course often want to do in
a database and you can also pass some options to find
and again, we'll take a close look at all these options and filters throughout the course too.
You also got find one,
the difference here is that find finds all matching documents, find one simply gives you the first matching
document it finds. Update one is then useful for changing one piece of data
and just as we can insert many documents at once,
we also have update many. Now the update functions take three arguments because we can pass a filter to
narrow down which document or documents to change then the data describing the change,
so what should be changed
and again some options we can use to configure that process.
We also got replace one in case we just want to replace a document entirely with a new one.
There also is delete,
we can delete one or delete many documents in a collection and there we also pass in a filter to find
out which document or documents to delete.
Now let's see all these things at an example,
again having a look at our flight data here which could look like this,
basically the data we already worked with and there, let's say this is the data we're using on our application.
Our application might be a website where people working at an airport or at an airline can schedule
a new flight,
therefore they would create an entry in our flight data collection,
they can change information about this flight so they can update information,
for example if the aircraft changed, they can cancel the flight let's say so they can delete it and
they can also display flight information on their own website in the mobile apps for their passengers
and therefore, they need to be able or you need to be able to read the data from this collection.
This is our little example here using the data we already set up.
So let's get started and play around with this crud operations and our data.