WEBVTT

00:05.180 --> 00:06.990
Hi everyone and welcome back.

00:06.990 --> 00:12.980
So in this video we're going to be talking about how to create and delete a collection in a mango day.

00:13.050 --> 00:18.410
DB So we're going to talk about how we can create and destroy a collection.

00:18.480 --> 00:23.820
And this collection is just a bunch of documents stored together so a collection just stores a bunch

00:23.820 --> 00:25.070
of documents.

00:25.680 --> 00:26.940
OK so let's get started.

00:26.940 --> 00:33.390
And this is going to be a very practical lesson so let's let's see how we can do so in Mongolia D.B.

00:33.560 --> 00:38.780
D-B create collection name options is used to create collection.

00:38.820 --> 00:45.510
So if you want to create a new collection inside your database then you just use D-B up create collection

00:45.780 --> 00:48.150
and then name and options.

00:48.150 --> 00:51.000
So for the basics index is as follows.

00:51.000 --> 00:53.510
So you have name and the options.

00:53.610 --> 01:00.570
So the command name is the name of the collection to be created so we can call add any name like movies

01:00.600 --> 01:08.490
or customers or anything really just it's just a name for the collection options on the other hand is

01:08.490 --> 01:09.290
a document.

01:09.300 --> 01:16.980
So options itself is a doc and so it's going to be like a Jason and it is used to specify the configurations

01:16.980 --> 01:18.150
of the collections.

01:18.270 --> 01:24.300
It will tell us you can disk inside the options you can describe some of the characteristics for this

01:24.300 --> 01:25.200
collection.

01:25.200 --> 01:31.430
So for example the maximum size of this collection or the maximum number of entries.

01:31.440 --> 01:31.840
Right.

01:31.860 --> 01:33.510
Maximum number of entries.

01:33.510 --> 01:34.010
Right.

01:34.050 --> 01:39.030
So all of these things you can specify for the collection that you're creating inside of the option.

01:39.030 --> 01:42.020
And then we're going to see how we can do that in the next slide.

01:42.350 --> 01:47.760
Now parameter of the options parameter is options you don't have to actually specify.

01:47.850 --> 01:50.780
You only need to specify the name of the collection.

01:50.820 --> 01:58.140
But for the parameters you don't really need to specify them really you can just give the collection

01:58.140 --> 02:00.380
a name and that's it.

02:00.390 --> 02:05.270
Now let's see some of the available options that we can pass to create collections.

02:05.490 --> 02:08.660
So you can pass first of all kept.

02:08.670 --> 02:13.310
Now the field cap is either true or false 1 or 0 as well.

02:13.320 --> 02:20.610
If it's true it enables capped collections of capped collections a fixed size collection that automatically

02:20.610 --> 02:25.950
overrides its oldest entries when it when it reaches its maximum size.

02:25.950 --> 02:29.870
So if you specify true you need to specify the size parameter too.

02:29.880 --> 02:30.920
So what does this mean.

02:31.020 --> 02:34.270
Well let's say you specify capped as 3.

02:34.350 --> 02:39.430
So this means that this collection over here can only store 3 elements 1 2 and 3.

02:39.450 --> 02:46.290
So if you insert a new element that deletes a lot of the oldest elements in it so it deletes this element

02:46.290 --> 02:48.660
and it puts this new element over here.

02:48.660 --> 02:54.270
Now if you insert a new element then delete the second oldest one and inserts and you so it it acts

02:54.270 --> 02:58.760
like a q person if you going think about it as a kid you're inserting and deleting.

02:58.920 --> 03:01.750
So it's the last element.

03:01.830 --> 03:06.540
The oldest element is removed and the insert a new element and so on.

03:06.570 --> 03:11.500
So it's just exactly like a cue you insert from here and you delete from the other site.

03:11.880 --> 03:17.870
Now if you set this as true then you have to set the size parameter here as a number right.

03:17.940 --> 03:23.420
And you have it's basically specifies the maximum size in bytes for a cap collection.

03:23.430 --> 03:27.320
So if CAPTUS true then you need to specify this field also.

03:27.340 --> 03:29.720
OK so what is kept.

03:29.760 --> 03:32.630
So this gap this is the field kept over here.

03:32.640 --> 03:38.340
So if this is true then this tells us how many documents should we have in the collection such that

03:38.370 --> 03:41.490
if I exceed this number then I delete the oldest.

03:42.540 --> 03:43.680
The next option.

03:43.710 --> 03:51.300
Or that we can pass in the create collection function or here divvied up create collection is auto index

03:51.690 --> 03:52.340
ID.

03:52.470 --> 03:59.120
And it's a boolean and if it's true it automatically creates index on ID fields of s.

03:59.430 --> 04:04.910
So its default value is false so usually is the default value of this is false.

04:04.920 --> 04:08.030
But you can also turn it on to be true.

04:08.040 --> 04:14.760
There's also a max which is a number and it it's all of course all of these options are are optional

04:14.910 --> 04:19.530
and it specifies the maximum number of documents allowed in the cap ticklish.

04:19.560 --> 04:25.000
OK so it tells you this is the maximum number that you could allow in this collection.

04:26.150 --> 04:26.590
OK.

04:26.640 --> 04:30.800
So let's take an example and see how we can do it in Mongolia maybe.

04:30.810 --> 04:37.380
So first of all we're going to create it we're going to use a database test and we're going to create

04:37.380 --> 04:39.750
a collection called my collection base.

04:39.750 --> 04:50.680
Let's go back to our database Maunganui and then I'm going to write use d'Este and then I'm going to

04:50.680 --> 04:57.160
say D-B don't create collection that I'm going to give in the name of my collection

05:00.030 --> 05:00.860
and it tells me.

05:00.900 --> 05:01.540
OK.

05:01.560 --> 05:02.540
1.

05:03.040 --> 05:03.730
OK.

05:03.900 --> 05:09.410
So you can create a collection by using the command show collections for your member.

05:09.690 --> 05:15.540
We can see all the list of databases by clicking show on TBS but now we can show all the collections

05:15.540 --> 05:18.860
in the database by writing show Debbies.

05:19.160 --> 05:28.080
If I glitched sure Sorriso show name it shows all the databases and show collections shows you all the

05:28.080 --> 05:31.780
collections and then you would see that we have my collection.

05:31.830 --> 05:33.660
This is the collection here.

05:34.070 --> 05:34.690
OK.

05:34.800 --> 05:41.160
So the following example shows the syntax of create collection method with a few important options so

05:41.400 --> 05:49.200
in this example over here were padding we're naming the collection my column and we're setting kept

05:49.290 --> 05:50.500
to be true.

05:50.730 --> 05:57.260
Auto index I need to be true size to be 6 1 4 2 8 0 and Max to be 10000.

05:57.270 --> 06:02.460
So I'm not going to copy all of this I'm just going to let cap to be true and just to show you how we

06:02.460 --> 06:03.760
can best it right.

06:03.870 --> 06:10.400
But this is an example of using all the four options in the document that you set.

06:10.410 --> 06:17.940
So I'm going to go back and then I'm going I read the doc created collection and then for the name I'm

06:17.940 --> 06:20.780
going to call it my collection.

06:20.780 --> 06:28.890
And for the options I'm going to pass a document and then one of them is kept and I'm going to set it

06:28.890 --> 06:30.730
to true.

06:31.050 --> 06:38.210
And then I'm also going to set size so size that I'm going to set it to say sorry.

06:38.460 --> 06:39.230
OK.

06:39.350 --> 06:42.500
And so this one should be true here.

06:42.530 --> 06:44.600
And not between brackets.

06:44.700 --> 06:46.570
If I remember correctly yes.

06:46.590 --> 06:50.350
So this one shouldn't be with parentheses like these.

06:50.350 --> 07:00.110
So capped and true and then size and three and then maybe we can also put Max as 3 right so this is

07:00.120 --> 07:01.570
Max.

07:02.130 --> 07:05.010
And it tells us that indeed created collection.

07:05.040 --> 07:06.020
Ok 1.

07:06.030 --> 07:13.990
And then if I see show collection's I would see that I have my collection and my collection here.

07:14.000 --> 07:15.110
The second one.

07:15.470 --> 07:16.220
OK.

07:16.550 --> 07:19.460
Now among Mungo DBI don't need to create a collection.

07:19.460 --> 07:25.010
In fact Mongo D-B creates the collection automatically when you insert some sound document.

07:25.190 --> 07:33.920
So if you look here if I do D-B magical collections that insert and so I'm referencing some collection

07:33.980 --> 07:41.210
that I haven't even spit like created but I'm just saying D-B that magical collection of insert inserting

07:41.210 --> 07:42.270
something in it.

07:42.470 --> 07:53.960
So if I go back in there to my example if I do D-B dot and then magical collection not answered and

07:53.960 --> 07:55.910
I insert a document into it.

07:55.910 --> 08:06.380
For example name then this is new and I click enter then it will tell me and then I show show collections.

08:06.920 --> 08:11.360
Then you would notice here that this created magical collection automatically.

08:11.510 --> 08:14.520
So you didn't even need to use the command create collection.

08:14.570 --> 08:21.450
If you actually just access any like this if you do indeed be dot and then any new name wherever for

08:21.520 --> 08:26.870
our collection then it's automatically created for you without any options.

08:26.870 --> 08:34.340
Right so you can specify any options and then it inserts this document into it your name.

08:34.340 --> 08:35.970
This is new gig.

08:36.440 --> 08:37.740
So hopefully this is clear.

08:40.940 --> 08:41.220
OK.

08:41.220 --> 08:43.470
So how do we delete a collection.

08:43.470 --> 08:46.970
So we know how to create a collection using this function here.

08:46.980 --> 08:48.390
Create collection.

08:48.390 --> 08:50.040
Now how do we delete a collection.

08:50.040 --> 08:51.600
How do we drop a collection.

08:51.750 --> 08:56.350
So Mongul D-B has the function D-B collection that drop.

08:56.550 --> 09:00.860
So one collection here what we mean is is the collection name.

09:00.870 --> 09:01.100
Right.

09:01.100 --> 09:07.530
So what you call your collection and it's use and then you do dog drop and then is used to create a

09:07.530 --> 09:09.340
collection from the database.

09:09.360 --> 09:09.820
Right.

09:09.900 --> 09:15.660
So it basically drops this collection name from your native This whole collection and all the documents

09:15.660 --> 09:18.210
inside of it from your database.

09:18.370 --> 09:19.450
Let's take an example.

09:19.450 --> 09:24.390
So first we can check the available collections into our database.

09:24.390 --> 09:26.880
So let's say we use my D-B.

09:27.000 --> 09:29.740
So let's go use my D-B.

09:30.090 --> 09:30.830
OK.

09:31.110 --> 09:32.460
And then whoops

09:38.290 --> 09:41.820
and then we're going to show collections.

09:42.340 --> 09:48.020
So I'm going to go here and say show collections and then I don't have any collections right.

09:48.160 --> 09:54.320
But then I'm going to say now I want to insert a new collection so I'll insert this collection for a

09:54.360 --> 09:58.390
saucy.

09:58.490 --> 10:06.530
So what I'll do is to create a new collection D-B create a collection and then I'm going to call it

10:06.560 --> 10:08.120
deleted.

10:08.470 --> 10:19.340
And then what I'm gonna do is I'm going to do D-B dot delete dot drop so I'm dropping this this collection

10:19.340 --> 10:21.110
here and it shows me that it's true.

10:21.110 --> 10:26.120
So I've successfully deleted this collection now.

10:26.270 --> 10:32.160
So once we've done D-B my collection job this means that this collection over here has been deleted.

10:32.180 --> 10:32.710
OK.

10:32.870 --> 10:39.020
So again we can check the list of collections in the database and the drop method will return true if

10:39.020 --> 10:42.110
the selected collection is derived successfully.

10:42.110 --> 10:43.960
Otherwise it will return false.

10:44.060 --> 10:50.540
So if it is true that means that this this operation was successful and this my collection has been

10:50.540 --> 10:53.640
deleted from the database so hopefully that makes sense.

10:54.850 --> 11:00.760
Now another important thing to talk about is the data types in the Mongo the collections or the documents

11:01.140 --> 11:06.970
no longer baby be supports many data types some of them are strings and this is the most commonly used

11:07.180 --> 11:09.320
data types are the data and string.

11:09.370 --> 11:13.850
In Mongolia we have to be UTF 8 valid.

11:14.170 --> 11:22.870
You can also store integers boolean values so integer or there are just 32 bits or 64 bit integers based

11:22.990 --> 11:26.310
depending on your server and you have booleans.

11:26.560 --> 11:33.110
And this is used to store a boolean true or false value value right 1 or 0 double wishes.

11:33.280 --> 11:35.820
Used to store floating point values.

11:36.130 --> 11:37.890
Many slash Max.

11:37.990 --> 11:43.580
And this is used to to compare a value against the lowest and highest being so an element.

11:43.580 --> 11:45.800
OK so that's cool.

11:46.420 --> 11:51.940
Now other types of data are like arrays and we've seen an example of this when we have a document like

11:51.940 --> 12:00.730
this and then we have three or if your memory was called tags and then on the other side there's an

12:00.790 --> 12:07.040
array write off tag 1 tag 2 and so on.

12:07.140 --> 12:07.620
OK.

12:07.640 --> 12:10.010
So this is an array here or here.

12:10.720 --> 12:15.310
And this type is used to store arrays or lists or multiple values into one key.

12:15.340 --> 12:17.590
Right so this is the key here tags.

12:18.100 --> 12:23.560
Also we have a time stamp so soci time stamp and this can be handy for recording when a document has

12:23.560 --> 12:28.590
been modified or or added you might have an extra key here to say.

12:28.720 --> 12:32.120
Last edited for example.

12:32.680 --> 12:39.070
And then you could put here a time stamp and that tells you when was the last time this document was

12:39.370 --> 12:42.260
was touched or or accessed.

12:42.280 --> 12:48.130
And we also have objects so this this data type is used for embed documents and no.

12:48.160 --> 12:50.360
And this is just to store a null value.

12:50.590 --> 12:56.030
So just nothing and simple this data type is used two identically to a string.

12:56.110 --> 13:00.370
However it's generally reserved for languages that use specific symbols.

13:00.370 --> 13:08.620
For example if you're going to write in Arabic or German or French maybe have some extra characters

13:08.620 --> 13:12.200
or signs above the letter.

13:12.280 --> 13:20.020
So for example if you write something like this then this this is reserved for these languages for symbol

13:21.910 --> 13:22.240
OK.

13:22.240 --> 13:27.280
Another thing is dates you can store this data type is used to store the current date or the time in

13:27.280 --> 13:28.360
Unix time.

13:28.480 --> 13:34.510
So it's not just any data it's in Unix type format and you can specify your own day time by creating

13:34.510 --> 13:41.530
an object of date and passing day month and year into it and then it automatically changes it to Unix

13:41.560 --> 13:42.300
time.

13:42.730 --> 13:48.000
There's also an object id and this data that is used to store the documents ID which is this one if

13:48.000 --> 13:55.340
you're a member underscore ID there is binary data and this data type is used to store binary data code

13:55.920 --> 14:01.030
within this data type is used to store javascript code into the documents you can actually store some

14:01.300 --> 14:04.210
code javascript code inside your document.

14:04.210 --> 14:05.330
So that's pretty go.

14:05.440 --> 14:08.860
And also regular expressions so rhetoric's for short.

14:08.950 --> 14:13.970
So redneck's and this type is used to store regular expressions.

14:14.290 --> 14:19.930
So lots of stuff lots of data types that you can store inside a document.

14:20.600 --> 14:27.080
Now to insert a document to a collection to insert data into maladie collection you need to use the

14:27.080 --> 14:33.590
insert function which you might have seen me use before or the Save method.

14:33.650 --> 14:36.610
Now the basic syntax of insert command is as follows.

14:36.610 --> 14:43.390
So it's D-B collection name dot insert and then the document itself that you want to insert.

14:43.520 --> 14:53.250
So for example the dot movies that insert and that insight that I'm going to have a document which basically

14:53.370 --> 15:00.180
the document I want to inserts the stuff in it right and then close parentheses and then that's how

15:00.180 --> 15:02.220
I construed to document inside of it.

15:03.420 --> 15:11.190
OK so here's an example so divi that my column insert and then for the ID I'm going to initialize it

15:11.190 --> 15:12.990
as an object ID.

15:13.110 --> 15:19.700
This one over here for that title I went how long would he be overview for the description.

15:19.690 --> 15:27.080
I'm going to say Mangu the non-sequel database for the tags I'm going to write an array of Mangu the

15:27.140 --> 15:29.800
database and no CQ and the like.

15:29.790 --> 15:31.620
So I'm going to put 100.

15:31.920 --> 15:34.510
So I've inserted this document here.

15:34.880 --> 15:39.740
And so when once I click enter then this is automatically inserted.

15:39.750 --> 15:46.210
So let's go back and try to insert this document into the example here.

15:46.440 --> 15:49.280
So is log in.

15:49.690 --> 15:52.730
And then you notice here if we write d.b.

15:53.100 --> 16:03.330
So let's see show the BS and then let's use test show collections and then we have my collection.

16:03.330 --> 16:14.350
So let's go for the big dot my collection Dot answered right and then I'm going to insert loops.

16:14.610 --> 16:18.060
So it's inserted an empty one.

16:18.090 --> 16:20.530
I meant to press shift and hurt but OK.

16:20.700 --> 16:27.900
So you're going to enter here and then I'm going to enter for example let's see I'm going to enter username

16:29.010 --> 16:34.190
and then ABC and then for a password.

16:34.410 --> 16:43.560
So when I say a password and then I'm going to say x y is it right and then once you're right then it

16:43.560 --> 16:47.760
tells you that it inserted one document here.

16:48.030 --> 16:49.680
So really this makes sense.

16:50.100 --> 16:55.830
So if you want to insert multiple documents in a single query you can pass an array of documents in

16:55.860 --> 16:57.270
insert command.

16:57.360 --> 17:00.390
So you don't actually have to insert only one.

17:00.390 --> 17:09.240
So if I go back here I can actually do D-B got my collection insert but instead of passing one document

17:09.240 --> 17:14.570
I can do an array of documents so I can pass several documents over here.

17:14.580 --> 17:21.180
Right so all of these are empty documents so I can insert them and they'll tell me that it did bulk

17:21.240 --> 17:28.860
right there and it inserted so and inserted as force inserted for empty documents into my collection.

17:29.040 --> 17:31.930
And he'll tell you basically the result of everything.

17:31.950 --> 17:32.410
Right.

17:32.580 --> 17:33.840
So there's no errors right.

17:33.840 --> 17:34.800
Errors are easier.

17:34.890 --> 17:42.740
So you can insert more than one collection into your database at a time using just the array notation.

17:43.100 --> 17:47.740
So you will stop here and in the next video we're going to talk more about mongered you.

17:47.840 --> 17:50.300
So until the next video Ibiquity.
