Now what we did in the last lecture where we updated the matched array element can also be done to multiple
matching elements.
So if I again find some documents based on an array based query, so where I maybe find hobbies with a frequency
that is greater than two,
so now I should find way more documents if I pretty print this, I find a bunch of documents and again always
remember, the document is the overall person that documented the collection, not the document
you filter for in your query, that is just the key factor for giving you back to the overall document.
So we found Maria because she has a hobby with a frequency greater than two,
same is true for Chris,
he also has a hobby with a lower frequency but that does not matter,
not all has to match,
he has at least one that matches,
that's the thing. We also found Anna and Manuel and Max,
so we found all documents. However not all array documents fulfilled our filter,
we had greater than two,
so for example cars for Manuel did not fulfill it
and so did hiking.
Now let's say we want to change all embedded documents in the hobbies array that did fulfill it.
So this was our query,
it gave us back all documents in our collection,
now let's update many documents
and here, I will just repeat my query, so hobbies frequency greater than 2
and now I want to update them, whoops
I pressed enter accidentally,
so now I want to update them. Again
we can use $set inc
and so on
but here, I want to set a new field value, so $set is the right option here
and I want to change all these matched hobbies.
So before, we had hobbies.$ and then we could override this as a whole value or add a new field
or edit an existing field with another dot notation.
However this only matches or edits the first matching hobby for each person
and if there are multiple matching hobbies per person, it will not update them all.
I can show this to you by simply adding another field, good frequency and setting this to true.
If I hit enter, it modified all five documents
but now if I find all my persons, whoops and I pretty print that, Maria has a good frequency,
Chris has it but note that
these two documents were not changed
and especially for this one, it's strange because the frequency of three is certainly higher than the
frequency of two which we looked for, only the first element was updated. Now for Anna it made sense that
the first one was not touched because frequency of two did not meet our criteria,
this one was updated though, for Manuel the same is the case,
the second one shouldn't be updated because the frequency of two is not fulfilling our filter criteria,
for Max however, again the second document just wasn't edited even though it should have been.
Well it should have not been because dollar sign as we used it simply refers to the first match.
Now sometimes you want to edit all elements in an array
and for now, let's ignore a filter,
so let's say not the ones that match some filter but you simply want to update all elements in an array.
Now for this, you can of course again update many
and let's say we now want to find all persons with an age older than 30, so age should be greater than
30,
let's quickly pretty print that,
whoops we
first of all find them not update them and of course it should be total age, we renamed that,
so we find Chris and we find Manuel.
Now let's say for these people, I want to add a new field to my documents here, to all documents in hobbies
not just to one. To do that, I can again use update many and then repeat my filter criteria,
so total age should be greater than 30 and then to update them, I can of course use $set,
I could also use inc and so on,
let's actually use inc to match it up, so I can use inc here
and then here, I want to update my hobbies but I don't want to override the entire field,
I just want to add a new field to every document inside of hobbies.
Now let's try accessing frequency with hobbies.frequency and increment it by minus one
because these people should slow down maybe or do more,
whatever you want but let's decrement it.
If I hit enter, I get an error,
cannot create field frequency in an element hobbies.
Now the problem here is that hobbies is an array and not a document itself and even though you can find
elements with this notation, you can't update
but you can update all array elements with another placeholder provided by mongodb and that
placeholder is
${}, whoops, $[].
This simply means update all elements and for each element, simply the frequency field because we have
embedded documents in our case,
if you hadn't embedded documents, you could of course stop after this part. And now if I hit enter,
you see two documents were modified and if I find my documents again, you see the frequency here for
example is zero now and previously it was one,
so all the frequencies actually decreased by one.
So this is the
$[] syntax for updating all array elements.