Now we worked a lot on updating elements in arrays,
now let's also work a bit on adding and removing elements to and from arrays. Now for this, we also
got some helpful methods we can use.
Now let's say we want you edit Maria so we can of course update one to mix it up,
find Maria by name and to update her, I can of course set a value here
and I could set hobbies to,
well to what? I can set it to a new array but I want to keep the old hobby which was good
food and only add a new one.
To do that, we can use a different operator, instead of $set which we still can use on different
fields
but instead of that for our planned update, I can use $push to push a new element onto the array.
I can do that by now specifying a document where I describe first of all the array to which I want to
push, hobbies and then the element I want to push,
so in my case, an element with the title of let's say sports because Maria is sporty and then a frequency
of let's say two.
So with that and that's one curly brace too much, with that if I now hit enter, you see one document was modified
and if I simply find my users, we see Maria still has good food which she had before but now
she also has sports, so that's push.
Push can also be used with more than one document,
so if I repeat that and I get rid of that one document, we can also use a different syntax here and
assign an object where I actually use $each which then is an array of multiple documents that
should be added
and here I could have more hobbies. I could have a title of good wine, fitting the good food I guess, frequency
of one because we want to drink responsibly
and then I add another hobby with a title of hiking and a frequency of two.
Now both these elements will be pushed onto the hobbies array,
now before I hit enter, let me show you two things you can add. For one, you could hit enter here,
that will work but you can now also add sort, $sort
and technically, we're still in that same object where we had $each,
so it's a sibling to $each, sort is separated with a comma after the array which is the value for
$each. Sort then describes how the elements in the array here should be sorted before they are
pushed into hobbies.
Now this might seem redundant because you could of course say well I can simply type them in the correct
order here but always keep in mind that might be data that is coming from your users and maybe you want
to have a sorted order in your array,
so if your users enter ingredients to a recipe in random order, that might be fine but you want to have
alphabetical order,
so at server side, you can simply push all user entered values but always specify a sort
so that you always have a sorted array.
So therefore here I can say I want to sort by frequency in descending order to have the highest frequency
first.
You can also specify a slice and say only add one element of the two here,
well it doesn't make much sense for my custom data
but again you could have a case where your application is sending you a bunch of data but you know hey
I want to sort and then I only take the first three elements and the rest doesn't have to be stored.
If you have such a use case, you can add slice here,
I will not add it,
I'll just go with each and sort. If I now hit enter,
one document was edited and if I search for Maria, you indeed see we have good food, we have
sports and then we have hiking and good wine added in this order,
so hiking comes first even though in my command, you see good wine came first but thanks to sort, this
was re-sorted.
Now if I would execute this again which I can do of course, then you see it's again sorted
and it sorts as you can see
the overall hobbies array, not just the hobbies it adds but also the existing hobbies which is great because
then you always have a sorted array here
no matter how many values you add.