Now being able to push new values is nice but due to my little example, we now got some duplicate data
here, hiking and good wine is in there too often. We can of course also pull data from an array,
so what I can do here is I can again update one document and that will be Maria,
so name Maria but here, I
then don't use push
but pull. Now pull takes an object where I describe how I want to pull or what I want to pull and here
I can say from the hobbies array,
I want to pull based on a condition which I then specify here,
I want to pull every element where the title is equal to hiking
let's say. With this command, if I now hit enter, you see one document was modified
and if I now find all documents, we see Maria has no hiking in there.
So of course we can now repeat that with good wine to get rid of that too,
just like this
and of course you could not just search for equality here, you could use greater than, all these normal
filter operators you know,
so now the good wine was also removed from Maria.
Sometimes you also just want to remove the last element of an array,
so you have no specific filter criteria, you just want to remove the last element. For that, let's mix
it up and let's update a document where the name is Chris and for Chris, you see the last element
is hiking,
so let's say we want to remove that and we can do that with the
$pop operator here. You then simply define the name of the field of which you want to pop, hobbies
for example.
Now the value here defines if you want to remove the last element, that would be done if you add one
here
or if you actually want to pop off the first element which you could do with -1.
So let's do one, it modified one document
and if I find all users, you'll see that for Chris,
indeed there are only two hobbies and hiking which we previously had with a frequency of zero is gone.
So that is pop which is great if you want to remove the first or last item of an array.