Saltar navegación

Activa JavaScript para disfrutar de los vídeos de la Mediateca.

Back end vs Front end - Vídeo 7 - Contenido educativo

Ajuste de pantalla

El ajuste de pantalla se aprecia al ver el vídeo en pantalla completa. Elige la presentación que más te guste:

Subido el 9 de julio de 2024 por Mario S.

72 visualizaciones

Modelos de datos para guardar en mongoDB. mongoose.Schema(). mongoose.model(). Validaciones y tipos.

Descargar la transcripción

Well, in this video and in the next two we are going to create, on the one hand, 00:00:00
the scheme and the model of the data that we want to save in our MongoDB. Remember that 00:00:11
Mongo allows us to save collections, documents with different number of key value fields, 00:00:18
ok and that in some way we have to validate it because if not it would be crazy then 00:00:29
on the one hand we are going to do that the scheme and the model on the other hand we are going to do the different 00:00:36
routes remember that the development that we are doing on the one hand we are going to see how to log or 00:00:42
register a user on our server and on the other hand once that user exists how 00:00:52
can save, create, eliminate different tasks, then in this video what we are going to see is how to 00:00:58
make those schemes and these models, we will see some types of personalized validations, well 00:01:09
personalized it gives us the mongoose itself and that's it, then here we are going to come to the 00:01:18
models folder and in models we are going to create a file that obviously you can put the name that 00:01:28
you want, okay, I'm going to put the user model, we're going to put it all in the lower case, a better 00:01:35
user point model point j s is always j s as we are doing so far, it is worth using the 00:01:53
model point j s I know because what I am going to do here is the scheme and then passing the model 00:02:02
of mongoose of the data when I register a user is worth then because of there the user model 00:02:09
because it is the model and js because it is javascript is worth then we are already in our file in white 00:02:20
we start to work on it as I told you to make that template that scheme of the values that 00:02:27
we are going to put in this document because we are going to use mongoose ok then we are going to 00:02:38
as always first we import what I need to import because mongoose mongoose from mongoose 00:02:48
What is the next thing I do? Well, we are going to generate the scheme, the scheme, there you are, okay, the scheme, the 00:03:01
scheme of those data then this is worth if we go to the technical documentation of mongoose 00:03:16
which is what I have already told you in several videos you have to get used to using that documentation 00:03:31
we can see some changes as it can be, for example, well here we see the validations that 00:03:40
now I will use them, it is worth if I come to populate because for example here this is something we go the same 00:03:53
that we are going to create with the skin and in the end what we are going to do is convert 00:04:01
to a good model then look like here instead of the import or the export what it says is the 00:04:07
conch mongoose is equal to the request of mongoose this is the same as the import mongoose from mongoose 00:04:14
is worth and then instead of using the mongoose point skin as I have said is that what you do is 00:04:22
with between keys that function of mongoose that I want to use and where I get them from mongoose 00:04:32
ok and from there everything would be more or less the same you see that there is an idea a name a good age here 00:04:39
there is documentation that is information as it cannot be otherwise that it is interesting that you also 00:04:46
learn to use it, then well, having said that, I go back to our application and we continue 00:04:52
working then what fields am I going to ask or what camps and what fields are we going to ask our 00:05:06
client that he gives us to register is worth then on the one hand it can be typical the user name 00:05:14
on the other hand because the password we are going to put the first gmail 00:05:26
e-mail, we are going to put the password, 00:05:35
well, we would put the fields that you can think of, it is worth using the user name, the e-mail and the 00:05:54
pass because they are the classics and in this example that we are doing because they are the ones that are 00:06:01
usually used ok then what is what I am putting now because the type of data the restrictions 00:06:09
that have valid restrictions no good validations ok that we are going to give ourselves then that 00:06:17
I am going to put it between keys here I now put the fields that are worth within the 00:06:24
properties of the field and use name after the key I put an important comma if not it will give you 00:06:36
an error here I do the same and here I do the same 00:06:43
and it is good that it is the first the first property that I have to say among us of these fields because the 00:06:53
the field type, then the user name, the email and the password will be of the string type but 00:07:07
you should know that types of data can also be used because the name of the date is going to be a 00:07:14
boolean an array is worth there are several types of data that you can have them all 00:07:21
in the documentation of mongoose, okay, in principle, we are going to use 00:07:33
the stream type ones, okay, so I'm going to pass the type string, okay, I'm going to pass the 00:07:43
type the type string and we are going to pass here also the type this is worth then this 00:07:57
say the type you have to say yes or yes ok for example validations because we are going to put 00:08:15
here a comma and I am going to put it because the typical validation of the required or not is 00:08:26
required that field is valid then because that for example in the user in the email and in the password 00:08:34
the normal thing is that those fields are required, it is mandatory that the user gives us that 00:08:41
information then it is required two points we tell the other and also 00:08:48
and also 00:09:09
ok what other validation can we put for example there is another one that is the trim tr and m that 00:09:10
what it does is remove the blank spaces that are before and after the string chain that 00:09:28
the user is going to pass to us ok then that is comfortable then here I am going to put 00:09:33
32 points equal to you and we are going to put the comma here that I had forgotten and here we are going to say 00:09:40
here I am missing the comma also in the password because I am not going to say anything about the spaces that 00:10:02
I removed them before or after, then we can put for example because the email in our 00:10:17
database is unique, it is worth then we use the validation and unique 00:10:25
vale y está como no me hace falta vale y esto sería nuestro skin vale nuestro el esquema vale 00:10:30
que es como se utiliza esa función de mongoose ahora esto yo lo quiero pasar a un modelo de 00:10:55
mongoose to be able to work from anywhere part of my development with this model is worth 00:11:01
then what am I going to do first as we have been doing so far I am going to declare 00:11:09
a variable with which I am going to call for example out and use or out instead of using I am going to call 00:11:19
the skin I am going to call it like this to another scheme this is wrong written ok ok and now what I am going to 00:11:31
do is this this scheme I have to export it and to export it you have to convert it first 00:11:45
mungus tells you to convert it first to a model and that is done to export it you already know that 00:11:51
is the sport of paul we are going to call this good before we put the name that we wanted to export 00:12:01
here what I want is to export a model of mongoose then here I have to put mongoose point model 00:12:11
ok and in the parameters that you have to pass here is first the name the name the string is worth 00:12:17
as the name is being said that I want to give it then, for example, here I am going to give it 00:12:25
and that is with the name that I am going to make reference from anywhere part of my development is worth 00:12:32
of my back-end to work on these schemes is valid then also to the different 00:12:42
values ​​that the users have passed us is worth then we have the user and on the other hand we have 00:12:48
the out is that out is worth only what it does is good because it links it says the user model is based 00:12:55
on the out scheme scheme is worth and in this way I can already use that scheme in any part of 00:13:10
my development, it would be worth doing the same, I would have to do another js for later tasks and we will do it 00:13:19
For now, this is how Mongoose works, the part of creating a data scheme and then making the Mongoose model to be able to work on that scheme. 00:13:26
Idioma/s:
es
Idioma/s subtítulos:
en es
Autor/es:
Mario S.
Subido por:
Mario S.
Licencia:
Dominio público
Visualizaciones:
72
Fecha:
9 de julio de 2024 - 17:52
Visibilidad:
Público
Centro:
IES VILLABLANCA
Duración:
00′ 12″
Relación de aspecto:
1.78:1
Resolución:
1920x1080 píxeles
Tamaño:
35.56 MBytes

Del mismo autor…

Ver más del mismo autor


EducaMadrid, Plataforma Educativa de la Comunidad de Madrid

Plataforma Educativa EducaMadrid