For this I started my mongod server again and I connected to it again and I got nothing in there.
Now if you have something in there and you want to get rid of that,
you can always use a database and then simply run db drop database to get rid of the database and all
the data that is in there
and as you see, you can even run that on databases that don't exist and it will not complain.
So then you should be in that same state and have absolutely no data in there except for the default
databases which you need.
Now with that, let's insert some persons again,
so let's say we have a persons collection or let's first of all switch to a database,
we could name it contact data and then there, let's use the persons collection and
let's insert one person.
Now you learned this before
but in the end here, you enter one document, a document always is marked by the curly braces that's around
it and then a document is made up of the key value pairs,
so you have these fields like name is the field, the value for this field is Max and then the next field is
separated with a comma like
age 30,
in this case this is a number
hence we have no quotation marks around it
and then we might also have some hobbies which could be an array,
sports and cooking. And if I insert this, we get this auto-generated ID here which is guaranteed to be
unique and which also has this temporal component,
so if you have multiple documents, they are by default ordered after this ID because the ID reflects
when they were created.
So if I add another document here, let's say Manuel and I set the age to 31
and I set the values here to cooking and let's say cars, then I inserted this document too,
ok.
So this is insert one,
now let's insert many and for this I'll reach out to the same collection and there, I'll use insert many
like this
and now the important thing here is you don't enter multiple documents separated by a comma because
you don't pass them as arguments to this method.
Insert many does indeed have a way where it accepts some data after this comma, so a second argument and
I will dive into that second argument in a second
but if you want to insert multiple documents, you just have to pass an array of these documents,
so a list of these documents, a comma separated list.
So then we could insert Anna, age 29 and there maybe add some hobbies
and as you learned in the schema module, you don't have to have the same structure on all documents in
the same collection,
this is totally optional
so it would be totally fine if you inserted Anna without hobbies but I'll insert Anna with the hobbies
of sports and yoga and as you see, that worked too even though it was only one person but of course the
main idea behind insert many is that you do enter multiple persons or multiple documents but you don't
have to as you just saw, it's perfectly fine if you use insert many with one document only,
you still need to put it into an array,
it does not work otherwise.
So if I now enter more persons here real quick and I'll leave out the hobbies to speed it up for those,
so here we also have Maria and we have Chris,
then here you see I inserted two documents separated by a comma also enclosed in square brackets,
hence it's an array and this also worked.
And with that, I got all these persons in the database and these are the insert methods you learned about.
Now as I mentioned, there also would be the insert method, the insert method can still be used,
it's just not recommended.
Insert works with both a single document,
so if we insert Phil here, this does work and you see the output is a bit different, we get a different
result,
now this does not mean that Phil didn't get an ID.
If we quickly have a look into persons with find and pretty, we see that Phil does have an ID, so this
output here just does not tell us the ID which also is an advantage of insert one and many,
they give us a different output which is a bit more meaningful or helpful because it immediately gives
us the ID in case we want to work with it,
we don't have to query the database just to get that ID, which by the way is a real advantage because
if you're building an application where you insert some data into the database, then often you want to
get that ID back of that insert operation and immediately use it in your app.
Let's say you build a user interface where users also can delete documents or objects they created, then
you need that ID in your UI, in your frontend application so that if the user clicks the delete button,
you already know which ID,
so which document with which ID to delete.
If you have an insert operation, that does not tell you the ID,
it basically means that you need to wait for the insert to complete just to then query all documents and
find that ID manually, so that your frontend continues to work.
So this is a real thing,
this inserted ID,
it's not just a nice benefit, it's extremely helpful.
So this is also another reason why insert is not that great
but the other thing of course is we can use it for both a single person as you just saw or for multiple
persons by adding an array here.
So if I have Sandeep here with an age of 28 and I also have Hans here with an age of 38, then this
also works, now you see it automatically did a bulk write,
it gives us a different output here too,
it still doesn't tell us the ID but on the other hand, we get useless results like that it did not remove
any elements,
well I did not expect it to do that on insert and therefore, insert one and insert many are definitely
more helpful both from output perspective but also from an input, from a readability and error protection
perspective.