The obvious question that remains is how can I now find a certain set of persons, a certain set of documents
and hobbies and only update those?
Let's say we want to find all hobbies with a frequency greater than two,
so first of all we want to find all documents where the persons have hobbies with such a frequency
but thereafter, we also want to change the elements in the array.
Now let's first of all find the right documents again with the help of find, here
we dig into hobbies and frequency,
here we can use that dot notation, in the filter it's allowed and we look for a frequency greater than
2,
let's say. Let's pretty print
that and we see Maria has one hobby,
here we also get a hobby with a frequency lower than two and also one equal to two
which also does not make it into the greater than condition
but here is the one fulfilling our condition and you have to remember that one fulfilling document here
gives us the entire surrounding document because we're still looking for the surrounding document. Here
this is the winner
and here, this is the winner
and here both documents are greater than 2,
the frequency is greater than 2.
So with that, I now want to update all hobbies where the frequency is greater than 2,
so this one should be updated,
this should be updated,
this should not be updated for example.
Now can we achieve that?
Well we do access are users and update many and now we first of all have to find the right persons,
so again hobbies.frequency
and here we look for a frequency greater than two, the same we had before.
Now in the update mechanism, I want to set something because I want to add a new field
and now there, I can access hobbies.$ would give me the first matching element,
this gives me all elements,
now what I can do here is I can define an identifier here and the name is up to you,
like el
and then define what you want to set as a value.
Now I will first of all add a nested document because this will refer to one document in the hobbies
array,
so in there I'll add a good frequency field and I want to set that to true.
Don't hit enter,
this will not work right now because this part will not be understood by mongodb, this el thing is
something we defined because now we add a third argument to the update many method which also is a document.
This is the document where you could set upsert to true, where you could also add a write concern
but I will set an array filters key here.
Now array filters works together with this syntax, here
you can define some conditions by which you want to filter elements and these conditions can even differ
from these conditions here,
so they don't have to be equal.
You could be looking for something totally different
here like age greater than 30 and then still filter out certain array elements in this array and you could even
do that for multiple arrays because with the el here, you set an identifier for the condition you want
to apply for this update expression here. Now in array filters,
you therefore have an array
of the different filters for the different arrays you might be updating.
There you have multiple documents, one for each filter and then you have to repeat your identifier,
so in my case it's el, if you named this differently which you can,
you have to name it differently here too.
Now for us here, el is one element in hobbies which happens to be an embedded document,
so if I want to include or check some fields, some field in that embedded document, I can use el dot and
then frequency here as an example and then check whether frequency is greater than 2.
With that I'm rebuilding this query up here
but you don't have to do that,
you can have totally different queries here.
You could be looking for the age up here and still look for elements with a frequency greater than two
here.
Now here I am missing a square bracket to close that
and on the other hand, I don't need these.
So now if I hit enter, you see it modified five documents, all five documents that met this condition
and if I now look into my users, you see Maria has a good frequency here, Chris does only have it here
because these two indeed did not meet our condition of a frequency greater than two,
here it was updated but not for this document,
here it was updated for this but not for this document and for Max and that's interesting, it was updated
for both because both met this criteria.
So this is a nice syntax which allows you to be really flexible about filtering out elements in an array
that should change
and as I mentioned, here we had the same filter and then array filter, you could have totally different
filters.
So this filter by which you identify documents and the filter by which you identify array elements you
want to update
do not have to be equal.