Hi everyone and welcome back.
So in this video we're going to be talking about how to create and delete a collection in a mango day.
DB So we're going to talk about how we can create and destroy a collection.
And this collection is just a bunch of documents stored together so a collection just stores a bunch
of documents.
OK so let's get started.
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.
D-B create collection name options is used to create collection.
So if you want to create a new collection inside your database then you just use D-B up create collection
and then name and options.
So for the basics index is as follows.
So you have name and the options.
So the command name is the name of the collection to be created so we can call add any name like movies
or customers or anything really just it's just a name for the collection options on the other hand is
a document.
So options itself is a doc and so it's going to be like a Jason and it is used to specify the configurations
of the collections.
It will tell us you can disk inside the options you can describe some of the characteristics for this
collection.
So for example the maximum size of this collection or the maximum number of entries.
Right.
Maximum number of entries.
Right.
So all of these things you can specify for the collection that you're creating inside of the option.
And then we're going to see how we can do that in the next slide.
Now parameter of the options parameter is options you don't have to actually specify.
You only need to specify the name of the collection.
But for the parameters you don't really need to specify them really you can just give the collection
a name and that's it.
Now let's see some of the available options that we can pass to create collections.
So you can pass first of all kept.
Now the field cap is either true or false 1 or 0 as well.
If it's true it enables capped collections of capped collections a fixed size collection that automatically
overrides its oldest entries when it when it reaches its maximum size.
So if you specify true you need to specify the size parameter too.
So what does this mean.
Well let's say you specify capped as 3.
So this means that this collection over here can only store 3 elements 1 2 and 3.
So if you insert a new element that deletes a lot of the oldest elements in it so it deletes this element
and it puts this new element over here.
Now if you insert a new element then delete the second oldest one and inserts and you so it it acts
like a q person if you going think about it as a kid you're inserting and deleting.
So it's the last element.
The oldest element is removed and the insert a new element and so on.
So it's just exactly like a cue you insert from here and you delete from the other site.
Now if you set this as true then you have to set the size parameter here as a number right.
And you have it's basically specifies the maximum size in bytes for a cap collection.
So if CAPTUS true then you need to specify this field also.
OK so what is kept.
So this gap this is the field kept over here.
So if this is true then this tells us how many documents should we have in the collection such that
if I exceed this number then I delete the oldest.
The next option.
Or that we can pass in the create collection function or here divvied up create collection is auto index
ID.
And it's a boolean and if it's true it automatically creates index on ID fields of s.
So its default value is false so usually is the default value of this is false.
But you can also turn it on to be true.
There's also a max which is a number and it it's all of course all of these options are are optional
and it specifies the maximum number of documents allowed in the cap ticklish.
OK so it tells you this is the maximum number that you could allow in this collection.
OK.
So let's take an example and see how we can do it in Mongolia maybe.
So first of all we're going to create it we're going to use a database test and we're going to create
a collection called my collection base.
Let's go back to our database Maunganui and then I'm going to write use d'Este and then I'm going to
say D-B don't create collection that I'm going to give in the name of my collection
and it tells me.
OK.
1.
OK.
So you can create a collection by using the command show collections for your member.
We can see all the list of databases by clicking show on TBS but now we can show all the collections
in the database by writing show Debbies.
If I glitched sure Sorriso show name it shows all the databases and show collections shows you all the
collections and then you would see that we have my collection.
This is the collection here.
OK.
So the following example shows the syntax of create collection method with a few important options so
in this example over here were padding we're naming the collection my column and we're setting kept
to be true.
Auto index I need to be true size to be 6 1 4 2 8 0 and Max to be 10000.
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
can best it right.
But this is an example of using all the four options in the document that you set.
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
going to call it my collection.
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
to true.
And then I'm also going to set size so size that I'm going to set it to say sorry.
OK.
And so this one should be true here.
And not between brackets.
If I remember correctly yes.
So this one shouldn't be with parentheses like these.
So capped and true and then size and three and then maybe we can also put Max as 3 right so this is
Max.
And it tells us that indeed created collection.
Ok 1.
And then if I see show collection's I would see that I have my collection and my collection here.
The second one.
OK.
Now among Mungo DBI don't need to create a collection.
In fact Mongo D-B creates the collection automatically when you insert some sound document.
So if you look here if I do D-B magical collections that insert and so I'm referencing some collection
that I haven't even spit like created but I'm just saying D-B that magical collection of insert inserting
something in it.
So if I go back in there to my example if I do D-B dot and then magical collection not answered and
I insert a document into it.
For example name then this is new and I click enter then it will tell me and then I show show collections.
Then you would notice here that this created magical collection automatically.
So you didn't even need to use the command create collection.
If you actually just access any like this if you do indeed be dot and then any new name wherever for
our collection then it's automatically created for you without any options.
Right so you can specify any options and then it inserts this document into it your name.
This is new gig.
So hopefully this is clear.
OK.
So how do we delete a collection.
So we know how to create a collection using this function here.
Create collection.
Now how do we delete a collection.
How do we drop a collection.
So Mongul D-B has the function D-B collection that drop.
So one collection here what we mean is is the collection name.
Right.
So what you call your collection and it's use and then you do dog drop and then is used to create a
collection from the database.
Right.
So it basically drops this collection name from your native This whole collection and all the documents
inside of it from your database.
Let's take an example.
So first we can check the available collections into our database.
So let's say we use my D-B.
So let's go use my D-B.
OK.
And then whoops
and then we're going to show collections.
So I'm going to go here and say show collections and then I don't have any collections right.
But then I'm going to say now I want to insert a new collection so I'll insert this collection for a
saucy.
So what I'll do is to create a new collection D-B create a collection and then I'm going to call it
deleted.
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
here and it shows me that it's true.
So I've successfully deleted this collection now.
So once we've done D-B my collection job this means that this collection over here has been deleted.
OK.
So again we can check the list of collections in the database and the drop method will return true if
the selected collection is derived successfully.
Otherwise it will return false.
So if it is true that means that this this operation was successful and this my collection has been
deleted from the database so hopefully that makes sense.
Now another important thing to talk about is the data types in the Mongo the collections or the documents
no longer baby be supports many data types some of them are strings and this is the most commonly used
data types are the data and string.
In Mongolia we have to be UTF 8 valid.
You can also store integers boolean values so integer or there are just 32 bits or 64 bit integers based
depending on your server and you have booleans.
And this is used to store a boolean true or false value value right 1 or 0 double wishes.
Used to store floating point values.
Many slash Max.
And this is used to to compare a value against the lowest and highest being so an element.
OK so that's cool.
Now other types of data are like arrays and we've seen an example of this when we have a document like
this and then we have three or if your memory was called tags and then on the other side there's an
array write off tag 1 tag 2 and so on.
OK.
So this is an array here or here.
And this type is used to store arrays or lists or multiple values into one key.
Right so this is the key here tags.
Also we have a time stamp so soci time stamp and this can be handy for recording when a document has
been modified or or added you might have an extra key here to say.
Last edited for example.
And then you could put here a time stamp and that tells you when was the last time this document was
was touched or or accessed.
And we also have objects so this this data type is used for embed documents and no.
And this is just to store a null value.
So just nothing and simple this data type is used two identically to a string.
However it's generally reserved for languages that use specific symbols.
For example if you're going to write in Arabic or German or French maybe have some extra characters
or signs above the letter.
So for example if you write something like this then this this is reserved for these languages for symbol
OK.
Another thing is dates you can store this data type is used to store the current date or the time in
Unix time.
So it's not just any data it's in Unix type format and you can specify your own day time by creating
an object of date and passing day month and year into it and then it automatically changes it to Unix
time.
There's also an object id and this data that is used to store the documents ID which is this one if
you're a member underscore ID there is binary data and this data type is used to store binary data code
within this data type is used to store javascript code into the documents you can actually store some
code javascript code inside your document.
So that's pretty go.
And also regular expressions so rhetoric's for short.
So redneck's and this type is used to store regular expressions.
So lots of stuff lots of data types that you can store inside a document.
Now to insert a document to a collection to insert data into maladie collection you need to use the
insert function which you might have seen me use before or the Save method.
Now the basic syntax of insert command is as follows.
So it's D-B collection name dot insert and then the document itself that you want to insert.
So for example the dot movies that insert and that insight that I'm going to have a document which basically
the document I want to inserts the stuff in it right and then close parentheses and then that's how
I construed to document inside of it.
OK so here's an example so divi that my column insert and then for the ID I'm going to initialize it
as an object ID.
This one over here for that title I went how long would he be overview for the description.
I'm going to say Mangu the non-sequel database for the tags I'm going to write an array of Mangu the
database and no CQ and the like.
So I'm going to put 100.
So I've inserted this document here.
And so when once I click enter then this is automatically inserted.
So let's go back and try to insert this document into the example here.
So is log in.
And then you notice here if we write d.b.
So let's see show the BS and then let's use test show collections and then we have my collection.
So let's go for the big dot my collection Dot answered right and then I'm going to insert loops.
So it's inserted an empty one.
I meant to press shift and hurt but OK.
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
and then ABC and then for a password.
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
tells you that it inserted one document here.
So really this makes sense.
So if you want to insert multiple documents in a single query you can pass an array of documents in
insert command.
So you don't actually have to insert only one.
So if I go back here I can actually do D-B got my collection insert but instead of passing one document
I can do an array of documents so I can pass several documents over here.
Right so all of these are empty documents so I can insert them and they'll tell me that it did bulk
right there and it inserted so and inserted as force inserted for empty documents into my collection.
And he'll tell you basically the result of everything.
Right.
So there's no errors right.
Errors are easier.
So you can insert more than one collection into your database at a time using just the array notation.
So you will stop here and in the next video we're going to talk more about mongered you.
So until the next video Ibiquity.