Now that we worked quite a bit on the different field operators, before we talk about array operators which
are nice operators that help us with working and updating array values,
let's dive into a nice option you can pass to update one and update many.
First of all, let's quickly have a look at our persons again.
These are the four documents we have, Max, Manuel, Anna and Chris
and now let's say we want to update some document where we are not sure whether it exists in the collection
or not
beause you have an application where you don't know if the data was saved to the database yet and if
it wasn't saved yet, you now want to create a new document,
if it was, you want to override the existing or update the existing document and that is something you can
do with db.users update one or many, works for both,
let's say we're looking for name Maria and Maria does not exist here,
important,
we got Chris,
we got Anna, we got Manuel, we got Max, we got no Maria here.
So we're looking for the name Maria and I want to update this value.
Now I will do that with $set
and here let's say I'll set the age to 29,
I'll set hobbies to one hobby to not type too much here, let's say title is good food, whoops, and the frequency
is three times per week whatever
and I also set isSporty to
true, like that.
Now if I hit enter, we found no magic document and hence no change was made.
Now as I mentioned, maybe in such a scenario, we don't want to get this information and then manually insert
that person but we want to let mongodb do that for us,
either update or insert. And you can do that by passing a third argument to update one,
so this is now not something you pass to your updating description but as a third argument to the
update one method and there, there is a nice option you can set which is called upsert and you can set this
to true,
the default is false and you don't need to set the default of course. Now upsert
simply is a combination of the word update and insert and means that if the document does not exist,
it will be created.
So now if I set this to true and I hit enter, you see it actually did not find anything, did not update
anything but it upserted a document with this new ID.
And if I now look into my objects, you see that this document was added
and please note that it even set the name to Maria even though I did not add this in my update one
operation, I filtered for this name
but when I set values, I only set the age, the hobbies and isSporty. Now it automatically set a name
because correctly mongodb assumed that for whatever you are filtering here, that this is something
it should try to set, if it's an equality filter, it should try to set or it should set on the inserted
document
because if you looked for this, this should probably be part of the new document and that is why it also
added name Maria here
even though I only specified these three values or these three keys. So upsert can be very useful for
working with updates where you don't know if something or if a certain document already exists.