Now that we added some data into our database in the last lecture,
let's come back to that json thing because actually, mongodb does not use json but bson on which
stands for binary json for storing data in your database.
Now you don't really need to care too much about that because all you will write and see will be json code like
we did it,
so this is json code,
this is what we use in the last lecture,
this is also what we see when we retrieve data from mongodb
but behind the scenes, mongodb actually uses bson data.
This conversion is done by the drivers,
so this basically takes your json code and stores it in binary data
and this is simply done because for one, it is more efficient to store than json data, so it's faster
and more efficient from a space, from a size perspective
and additionally it supports additional types because for example that objectId thing we saw in the
last lecture is not valid normal
json. If I were to go in to my editor which is visual studio code which you don't need but this is
simply an editor you can use for coding
and I would manually add an ID field here
and set this equal to type objectId with some text in there,
you see that my editor doesn't like this because it's not valid
json. Now it is not valid
json but mongodb can actually understand and take this and store it in its binary data
and there are some other types too
like for example you got different types of numbers with decimals and very big numbers
and these are stored in different ways
behind the scenes, you don't need to care too much about that
but this is something mongodb offers you, this special data type, other special data types and very
efficient storage
in general. We will always work with our data like this though,
so in json format and there's one other important thing you need to understand about this data.
If I add another piece of information into flight data with insert one,
so if I add another document and this time I'll type it manually, when adding data like this,
you can actually omit the quotation marks here as long as there is no whitespace in the name.
So we could also have departure airport like this and this would still be accepted so I don't need
to wrap the name into quotation marks here,
you can still do it to be very clear that this is json but well we know it's json, you can use it
like this as a shortcut, the value if it's a string still needs to be enclosed in double quotation marks though.
So if we had Berlin Tegel as an airport here and then let's say we have the arrival airport which
is heathrow,
then we could write it like this.
We can now also theoretically save it like this even though the aircraft, the distance and the information
whether this is an intercontinental flight is missing because you can have mixed documents in the same
collection. If you add the data like this, you still see, this is accepted
and if I now access all my flight data and find it and pretty print it, you'll see both documents exist
in there even though they have different schemas.
Now we'll dive a bit deeper into schema design and how we should structure our data in a separate module
but I also want you to take away and keep in mind that in mongodb, two documents in the same collection
don't have to have the same schema.
Often you will have at least some intersections or complete the equal schemas but it's not a must.
One other thing you have to know is that this ID which is auto-generated, you don't have to use
the auto-generated ID, you just have to ensure that you have a unique ID
but if you can ensure this, you can also assign IDs on your own. So if I add this same entry again with
departure airport and arrival airport,
I can add _ID in there as an extra field and it has to be _ID,
so not just ID, also not ID with a capital character, _id like this
and then you can use any ID you want, a number or
another string like txl-lhr-1. This can be done and you see this was also accepted
and inserted with this ID,
so if I again print all the results here, you see we got our two old results and this new one and this
new one does now not have an objectId which was generated for us by mongodb but our own ID. If
I try to enter the same entry again with the same ID though and that's the only thing which is the
problem here, not the other data,
the other data maybe equal as you can see here but if I use the same id again, we get an error
and in the end, this error is simply telling us that we get a duplicate key error in this collection
and that the duplicate key is the ID which is not allowed.
So this is the error we're seeing here and this is something you should also pay close attention to.