So now that it's clear that we are free to define a schema for our documents and to structure them in
exactly the way we want them to be structured for our use case or application, let's have a look at the
different data types we can use in mongodb,
so basically the types of data we can save in our fields, in our documents.
There we for example have text, we already saw text data types in this course, text always uses quotation
marks around the value and important here,
I'm talking about the values so the part on the right side of the colon in your json documents. So
you can save text like names,
you also got no hard limitation here,
the only limitation you have regarding the maximum amount of characters in your text are the 16mb
overall size for the whole document,
if your text is greater than that, well then your document becomes too big and your text is too long.
We also have booleans, booleans are simple values,
true or false, is something the case or not, is a user or an administrator or not,
you could save that as a true or false value and that value is called a boolean. Another core data
type
are numbers of course
and actually, we got a couple of different numbers in mongodb, we got integers for example and you see
int32 here because mongodb knows different kinds of integers, int32 integers are integers
that are 32bits long
and therefore if you try to store values that are longer than that, they will overflow that range and
you will end up with a different number, for longer numbers,
mongodb has a solution though, a long number, an int64 integer which you can also assign to store values.
In the shell by the way if you just enter a normal value,
it is treated as a float value,
that is because the normal shell as we use it in the course is based on javascript and javascript doesn't
differentiate between integers and floating point values,
so values with a decimal place,
therefore everything will be stored as a 64bit float value in the shell,
that is the default value and we'll play around with that a bit and see more of that in the numbers
module of this course
but be aware that mongodb is able to store smaller integers and bigger integers,
the solution you choose here of course kind of defines how much space will be allocated and therefore
how much space will be eaten up. And you also can store decimal numbers so numbers with decimal places,
I already said that the default in the shell is to store a floating point value,
you also got a special type, number decimal and that is provided by mongodb to store high precision
floating point values because normal floating point values also called doubles are basically rounded,
so they're not super precise after their decimal place.
For a lot of use cases, it's enough though, like for prices in a shop, you got enough precision for that
but if you're doing scientific calculations or anything like that, you might need a very high precision
and there is a special type that offers this, a very high decimal place precision where you got 34
decimal places,
so after the comma which are not rounded but guaranteed to be correct and precise. It's simply a computing
limitation that we got no 100% precision all the time
but that rounding occurs. Now besides these basic values even though some number values here already
are a bit more advanced, we also got the objectId value, the objectId object is a special object automatically
generated by mongodb to give you a unique ID which is not just a unique random string but also a string
that contains a temporal component, so that if you create two elements after each other, two documents after
each other, you are guaranteed to have the right order due to that ID because the older element will
have an ID that comes prior to the other one,
so there is this sorting built into the objectId because it respects a timestamp.
Speaking of timestamps, you can also save a date in your database, there is a special ISO date type which
you can well use, you can construct the date and save that to your database
and that is then a date which you can work with to do a date calculations and so on.
And there also is a timestamp type, the timestamp is mostly used internally,
you can create it automatically, mostly you let mongodb create that for you and that is guaranteed
to be unique too,
so even if you create two documents at the same time, they will not have exactly the same timestamp
because it will basically take into account the current time
and then also add an ordinal value, so that two documents at the same time still don't get the same time
stamp but respect the order in which the insert command was issued for example.
So you got that timestamp too and you could say that object, that the kind of is based on that timestamp
and then just uses some algorithm to spit out a seemingly random string.
Last but not least, you saw that you can embed other documents,
that's of course important too, you can embed documents and these documents can then in turn embed other documents
or hold any fields with any of the values you see here
and you can also have arrays as values and the arrays are simply lists of values,
you can have a list of strings, a list of booleans, a list of numbers or a list of other embedded documents
or even a list of lists, a list of arrays, that is possible too.
So these are the data types and throughout the course, you will see them again,
so we'll work with them
and for example there will be a whole module where I will work with numbers to give you a feeling for
why we have these different types of numbers. In the next lectures,
I just want to play around with these types a bit and give you a feeling for how we create the more
advanced types like dates here and how we can save them and work with them.