Ok, so in the last lecture you got a brief overview over the different insert methods we have and how they work.
I also briefly want to mention here that there are some other ways of inserting, one important alternative
can be seen in the update module because there will have an option to either update a document or insert
it if it doesn't exist yet
but this is it from the insert method world for now.
Now the more interesting part is now what you can do with these insert methods and there, you can actually
define or specifies some additional information.
Let's say we have our hobbies collection where we keep track of all the hobbies people could possibly
have and we insert many hobbies here.
Now each hobby is a document with the name of the hobby, like sports and we can have multiple such names,
cooking and then here also cars,
obviously it could hit enter now and it would work.
Now I did mention that we get this auto-generated ID, right?
Often you want to use that ID but it also is the case that you want to use your own ID,
maybe because the data is fetched from some other database where you already had an ID or maybe because
you need a shorter ID, here for example you could say the name of the sport also makes up for a great
ID.
So we could have _id sports here because we know that this will be unique,
we will not have sports twice in this collection, so we can absolutely use sports as an ID here and the
same here for cooking, we can of course also use cooking and I just name it all lowercase,
you could absolutely use a capital C in there, that would be accepted,
so this is not required to be lowercase but I'll use it like this
and the same for my last document, for the cars,
I'll set _id to cars. Now important,
It has to be _id, just ID will not work,
it has to be _id. If I now hit enter,
you'll see these were the inserted IDs and indeed when we look into the database, we'll see no other IDs,
so no object ID
and this is perfectly fine and this is not bad to do,
absolutely not, bring your own IDs if you have them,
so now I got these three entries in there and I got my own ID.
Now let's say I repeat that operation,
so I add more hobbies, I add let's say yoga here
and then let's leave the middle one untouched, I also add let's say hiking here. Now the middle one, cooking
is untouched because let's say the process where we add this, maybe it's user added data doesn't guarantee
that we check whether this item exists in the database before,
so I try to enter data with an ID that already exists in the database.
What happens if I now hit enter?
Well if I do this, I get an error,
now let's see what we got.
I get an error, a write error at item 1, arrays start with index 0,
so it tells us that the element at index 0, the first element succeeded but at index 1,
so the second element in the array it failed. So we failed in that bulk operation and it gives us some
more information,
it tells us again where it failed and that it failed because of a duplicate key error collection.
It could have also failed due to a different error, maybe our server went down during our operation,
that could have been a thing,
now here it failed due to duplicate key problems,
it also tells us the ID, cooking,
so it gives us the exact document on which it failed.
So this is of course helpful but you also see here that one element was inserted
and indeed if I find, whoops, if I reach out to my hobbies and I find them all and pretty print them, you see sports
was added,
cooking was added,
cars was added,
that was my first ride
but in the last ride where I got the error, yoga which was the first document was added to the collection,
so it failed after this element.
Now this is the default behavior of mongodb
and this is called an ordered insert and ordered inserts simply means that every element you insert
is processed standalone
but if one fails, it cancels the entire insert operation
but it does not rollback the elements it already inserted,
that's important but it cancels the operation and does not continue with the next document which as
we know would have succeeded.
So this is the default behavior, often
you want that behavior, sometimes you don't
and for these cases, you can override the behavior.
Now if I repeat that same operation, it will again fail
but now already on the first document because yoga was inserted in the last step, now it will of course
fail because now it's already in the database.
So how can I change this behavior?
Well we can pass a second argument separated with a comma to insert many, that argument is a document
and this is now not a document that will get inserted
instead it is a document that configures this operation.
And here we can set some options and the option that helps us in this case here is the ordered option,
the ordered option allows us to specify whether mongodb should perform an ordered insert which is
the default,
you can set this to true but that is redundant because data is the default setting,
so now it will behave in exactly the way we saw it before
but we can also set this to false and now it will not be an ordered insert
Now if I now hit enter, I still get that error
but you'll see I now get a list of all the right errors,
yoga and cooking which already tells us that it continued after failing on yoga
and we also see that one element was inserted and that has to be hiking
since we try to insert three and two of them failed. And indeed, if I now look into my database with find
pretty, we see hiking in there.
So by setting ordered to false here, we changed the default behavior and it is up to you what you require
or what you want in your application.
Be aware that it will never rollback the entire operation
if something failed, that is something I'll cover towards the end of the course in the transactions
module because we'll need some additional knowledge to work with such transactions.
A rollback will not happen here but you can control whether it continues with the other documents and tries
to insert everything which is fine or if it does not
and being able to set this is very useful because you might have cases where you can't really control whether
the data you are trying to insert already is in the database. And then an unordered insert with ordered
false can be your solution because you don't care about any documents that fail,
they already are in the database,
that's fine for you
and you can add everything which is not in there yet.
So this is how you can use ordered and unordered inserts to finetune the way documents get inserted
to your needs.