Back in the shell and still connected to that same mongodb instance we used earlier,
let's again check out our flight data collection with find
and as you saw, use find pretty to get a pretty output,
you see we got three documents in there.
We got the documents with our own custom ID,
we got the other document with incomplete information but the automatically generated ID and our original
document, we already saw insertOne.
Now let me actually start with clearing this database now before we then insert data again but this
time with insertMany. Now to clear the database, we have to use a delete command and we can simply use
our flight data collection because all these commands are always executed on a collection where you
want to add or delete documents and there, we got deleteOne or deleteMany.
Now here I want to clear all the data and for this, actually the easiest way would be to simply get rid
of the entire collection
but since that would be boring,
let's go with delete one to simply see how that works.
Delete one takes a filter to find out which document to delete
and now we could filter for all kinds of things.
A filter is defined as a document,
so with curly braces and then in its simplest form, you now simply define which key and which value
you want to delete or the document with that key and value you want to delete.
So for example here, we could say departure airport,
if that is equal to txl so to Berlin tegle, then I want to delete that document
and what this would do is it would go ahead, find the first document in our database where the departure
airport is txl and delete it.
So if I hit enter here, you see this was accepted, it was successful
and we see it deleted one document.
So if we now again use our flight data and find all documents and pretty print them, we only see two documents
now, the one with our custom ID and the first document we ever entered.
This means that this middle document was now deleted because it was the first document with the departure
airport of txl which it found,
obviously we don't have to use the departure airport,
we could have looked for an ID too. So I could also use db.flightdata.deleteOne and I could have also
looked for _ID and then maybe my custom ID, this one here,
txl-lhr-1,
if I do this, it would delete this document. But let me also show you how to delete many documents.
Now the thing is we got nothing in common,
if I just execute deleteMany like this, it fails though,
so I need to pass in a criteria.
Now we got nothing in common for our elements here,
so let's first of all update them real quick to add a new field to both documents so that they have something
in common, so that we can delete them in bulk, so that I can also show you this.
So for this, let's go back to the collection and now use update one, update one updates as the name suggests, one
element and for update one, we first of all define a filter where we say which document should be updated
and now let's say we want to start with this first element here.
So I want to update the document where in my case here,
maybe distance is 12000,
this should select this first document.
The second argument then is how I want to change it
and now the question is what do you pass there.
Now let's say we want to add a new field
and therefore we pass a document here, marker which hold some text, delete. By the way as you see here, you
can use single quotation marks for the value too
but you can also use double quotation marks.
Now this might add a new field marker with the value delete to that first element
but if I hit enter, I actually get an error and the error is that the update operation document must contain
an atomic operator or atomic operators.
Now this is a very valid error
and there's not really much you can do with it if you're just starting with mongodb.
The thing is it simply is an incorrect type of data for updating,
you don't pass in a document describing your change like this because Mongodb doesn't know how to interpret
this,
instead what you do pass here is a document with curly braces with a special keyword, $set
and this is something you can remember, whenever you see something with dollar sign in mongodb, it's
a reserved operator or word, $set is simply identified by mongodb when used in the update
one operation to describe the changes you want to make.
The value of $set then is a document with curly braces and we need to close another pair
of curly braces for our surrounding curly braces now marked in purple and now what this does is it tells
mongodb hey for this document you're finding,
please set this value and if this value did exist, it would change it to delete and if it does not exist
as in our case, it will add it.
So if I now hit enter, this succeeded,
you see it found one document that matched our filter and it modified one document then and now if
I again access my flight data, find it and pretty print it like this,
we see we now added the marker here.
Now let's say I want to add that marker to all my documents,
now what you can do then is you can use update many, so on flight data, you can use update many
and now you need to pass something as a filter but you can simply pass empty curly braces
and we could have done that on delete many too by the way,
this will select all documents. And now here, I again use set and then describe what to change
and now let's say I'll set the marker to toDelete here, so not delete but toDelete. If you hit enter,
you see it matched two documents and it modified two documents
and now if you again access your flight data, you find it and you pretty print it, you see that both
now have the marker to delete.
Please also note that for the first document, this was delete before and it was changed to toDelete
and now with that, we can use that marker to delete all.
Now as I mentioned, what we could do here is we could also use delete many and pass an empty pair of
curly braces too,
this should delete all elements but of course I also wanted to show you how to use update,
so here I will now use my new condition and check for all elements where the marker is to delete.
If we now hit enter, you see this was accepted and two elements were deleted and now if I access flight data
and I use find and pretty print this, well
we get back nothing because there is nothing in there anymore.
So this is how we can insert elements, how we can delete and update them and how we find them
but it's not all I wanted to show you.
Let's continue in the next lecture.