Now with the basic crud operations covered, let's dive a bit deeper and let's dive deeper into update.
First of all, let's have a quick look at all our data in the collection again,
we got these two flights and let's say I want to change the first flight,
I want to change it by adding an extra field which simply holds the information whether the flight is delayed
or not.
Now we can do that by accessing flight, whoops, flight data here and then using updateOne and I can filter by
ID, we can copy that objectId
and it's important that you copy the whole object and
we could filter for _id with this objectId.
Now this will select this first flight and now you learned,
we can pass a document with $set where we can tell what to change,
for example we can set delayed to true. If I now hit enter, this was accepted and if I output all the
flights again, we see the delayed was added.
So this is something we can do and something we already saw.
Now there also is another command we could use and that is not updateOne but just update,
so let me quickly remove the one here and now just use update. If I do that, you see it generally worked
but nothing was modified
and the reason for that simply is that we already had a value of true here,
so if I changed delayed to false, you see it did modify one element and if I now output all the data,
we see delayed is false. Now update works
a bit like updateMany, updateMany was used to update all matching elements
and update would also update all matching elements.
So what's the difference between update and updateMany then? The difference can be seen if I actually
remove $set here,
so if my second argument is now just delayed false in a document. Remember with updateOne,
this gave us an error and the same by the way is true
if you would use updateMany here, you also get an error here.
Now with update,
so just update without many or one, this actually works,
you see it was accepted and one document is changed.
Now how would you imagine that our flight looks like right now?
Well let's access the flight data
and find it all and output it
and what we see is we got two documents but the first one is very small because it now only has delayed
false and the ID. The ID still is the old ID by the way,
this was not changed, it still is the ID, did not change that but it overwrote all the other key value
pairs and that is the thing about update. Update does accept this syntax with just an object and it will
then take this object and basically replace the existing object with this new object, with this new document,
it will only keep the ID,
it will only patch the existing object instead of replace it, like updateOne and many do it
if you use the $set way, if you use it like this, you override it.
Maybe this is what you want but it is something to be aware of
and in general, I recommend using updateOne and many to avoid this issue.
If you want to replace something, you should use a different method, on flight data,
you should then use replaceOne
and there you again
place your filter,
so looking for ID equal to our objectId and then you add the element you want to replace it with as
a second value.
So here I'll take my flight data from the attached json document which was attached to earlier lectures,
copy that, delete the closing parentheses,
add my document and now close it and if I now hit enter,
you see one element was modified and if we now output all the elements again, you see the ID still is
the same as before,
it's the ID we looked for in the filter but the rest was replaced and this is simply a more explicit and
therefore safer way of replacing the data
if you really want to replace it
otherwise if you just want to change it partially, change it maybe, use updateOne or updateMany.
Be aware of the behavior of update.