So now we covered some interesting options for the write
or for the insert operation and the write concern is also applicable to updates and deletes.
Now there's one important concept I want to touch on and that is the atomicity of any write operation.
Let's say we have an insert one operation but this could be any write operation again,
now such operation
most of the time will of course succeed but it can fail,
there can be an error and here I'm talking about errors that happened whilst the document is inserted, so whilst
it's written to memory, whilst it's handled by a storage engine.
You could imagine that if you have a document with multiple fields, let's say also with a couple
of embedded documents, that some of these fields end up in the database,
so in the database files also whilst others are not included
because in between,
so whilst document was processed, the server had an issue.
So if you added a person with name, age and hobbies, maybe name and age was saved but not hobbies, mongodb
protects you against this,
it guarantees you an atomic transaction which means the transaction either succeeds as a whole or it fails
as a whole
and if it fails during the write, everything is rolled back for this document you inserted
and that is important.
It's on a per document level, that document means the top level document,
so it includes all embedded documents, all arrays so that is all included
but if you use insert many with multiple documents being inserted, then you don't get this,
so you only have support on a document level
but if you have multiple documents in one operation, like insert many, then only each document on its
own is guaranteed to either fail or succeed
but not insert many.
So if you have an issue during insert many, that just means that the documents on which it failed are
not added
and then the exact behavior depends on whether you used ordered or unordered inserts but the document
which already were inserted will not be rolled back
as I explained in the ordered and unordered insert lecture. We are actually able to control this on this
bulk insert or bulk update level too
and that will be related to a concept called transactions but that is something I will cover towards
the end of this course because it requires some additional knowledge about mongodb and how the
service work there that we don't have yet. But on a document level including all embedded documents, you
have this atomic operation guarantee.