1 00:00:02,330 --> 00:00:05,480 So now we covered some interesting options for the write 2 00:00:05,480 --> 00:00:12,260 or for the insert operation and the write concern is also applicable to updates and deletes. 3 00:00:12,260 --> 00:00:18,910 Now there's one important concept I want to touch on and that is the atomicity of any write operation. 4 00:00:18,950 --> 00:00:24,020 Let's say we have an insert one operation but this could be any write operation again, 5 00:00:24,260 --> 00:00:25,550 now such operation 6 00:00:25,550 --> 00:00:28,510 most of the time will of course succeed but it can fail, 7 00:00:28,520 --> 00:00:34,170 there can be an error and here I'm talking about errors that happened whilst the document is inserted, so whilst 8 00:00:34,190 --> 00:00:38,120 it's written to memory, whilst it's handled by a storage engine. 9 00:00:38,120 --> 00:00:42,290 You could imagine that if you have a document with multiple fields, let's say also with a couple 10 00:00:42,290 --> 00:00:49,640 of embedded documents, that some of these fields end up in the database, 11 00:00:49,640 --> 00:00:53,200 so in the database files also whilst others are not included 12 00:00:53,210 --> 00:00:54,410 because in between, 13 00:00:54,410 --> 00:00:58,260 so whilst document was processed, the server had an issue. 14 00:00:58,790 --> 00:01:04,700 So if you added a person with name, age and hobbies, maybe name and age was saved but not hobbies, mongodb 15 00:01:04,790 --> 00:01:06,340 protects you against this, 16 00:01:06,350 --> 00:01:14,000 it guarantees you an atomic transaction which means the transaction either succeeds as a whole or it fails 17 00:01:14,120 --> 00:01:14,860 as a whole 18 00:01:15,020 --> 00:01:20,820 and if it fails during the write, everything is rolled back for this document you inserted 19 00:01:21,050 --> 00:01:22,120 and that is important. 20 00:01:22,310 --> 00:01:26,940 It's on a per document level, that document means the top level document, 21 00:01:26,960 --> 00:01:31,650 so it includes all embedded documents, all arrays so that is all included 22 00:01:31,790 --> 00:01:37,190 but if you use insert many with multiple documents being inserted, then you don't get this, 23 00:01:37,190 --> 00:01:40,190 so you only have support on a document level 24 00:01:40,280 --> 00:01:45,800 but if you have multiple documents in one operation, like insert many, then only each document on its 25 00:01:45,860 --> 00:01:48,600 own is guaranteed to either fail or succeed 26 00:01:48,650 --> 00:01:50,110 but not insert many. 27 00:01:50,300 --> 00:01:56,090 So if you have an issue during insert many, that just means that the documents on which it failed are 28 00:01:56,090 --> 00:01:56,820 not added 29 00:01:56,930 --> 00:02:02,340 and then the exact behavior depends on whether you used ordered or unordered inserts but the document 30 00:02:02,360 --> 00:02:05,290 which already were inserted will not be rolled back 31 00:02:05,390 --> 00:02:13,370 as I explained in the ordered and unordered insert lecture. We are actually able to control this on this 32 00:02:13,640 --> 00:02:16,870 bulk insert or bulk update level too 33 00:02:17,000 --> 00:02:23,030 and that will be related to a concept called transactions but that is something I will cover towards 34 00:02:23,030 --> 00:02:27,890 the end of this course because it requires some additional knowledge about mongodb and how the 35 00:02:27,890 --> 00:02:33,860 service work there that we don't have yet. But on a document level including all embedded documents, you 36 00:02:33,860 --> 00:02:37,180 have this atomic operation guarantee.