Creation & Insertion in MongoDB
13K views
Oct 26, 2024
Creation & Insertion in MongoDB
View Video Transcript
0:00
creation and insertion in MongoDB. We know that MongoDB is a document-oriented database
0:07
So a collection will be containing multiple documents. So at first we shall concentrate on the
0:14
collection creation, then we shall concentrate on the document creation. So at first we are
0:20
considering the collection creation. To create a collection in the MongoDB, we can use the method
0:27
there is a create collection method. So, syntax will be db.d. Create collection. Here we're having
0:35
some name and then some options. So, let now let us discuss what is the name and what is
0:41
option. The name is the string type value and for the second argument, we can specify
0:47
some options. And these options are used to control the documents inside the collection
0:53
So, this is where mentioning the collection name and some options will be there
0:58
Now, let us discuss that what are the different options are available there
1:03
So, let me repeat once. So, db.crete collection is a method. At first we shall be passing the collection name and some options will be there So the options are like this So first one is a capped So cap is a field and the its type is bullion and it is optional if it is true
1:25
then enables the capped collection. Now what is the capped collection? So capd collection is a fixed
1:31
size collection. That means we are supposed to mention the size of the collection. So to make that
1:37
one, I shall have to make capped. That is a bullion option should be made true. The next field is
1:44
auto index ID. It is of the type of bullion. If this auto index ID, if it is true, then
1:52
automatically create index on underscore ID field's default value is false. So, its default value is
2:00
false, but if I want to make that one true, then the auto index ID will get created on this
2:07
underscore ID. Next one is the size and it is of the type of number. So, it is optional, specifies
2:15
a maximum size in bytes for a capped collection. So, when this capped, this bullion option is true
2:22
then this particular size will be working there Next one is the max So it is a number it is optional specifies the maximum number of documents allowed So these are the options that are available for the collection creation
2:39
Now let us discuss how to create one document. So document insert, so to insert documents in the collection, we have to use the insert method
2:51
Now what is the syntax? syntax is db. Collection name, dot, insert and then document
2:59
So, the documents should be in the JSON format. I think to know it better, to understand it better, let us go for one practical demonstration
3:08
where you will be issuing such commands and we shall show you that how the outputs are coming
3:14
So, here is the demonstration for you. So at first we are opening one terminal, so control alter T, we are issuing
3:22
command pseudo service mongo db start password is asked password is given then I shall issue
3:36
the command mongo to get the mongo shell so the mongo shell has been obtained so going for creation of our database so I shall go for show DBS you see the database is not pre so I going for my database so
3:54
switch to my database so now if I go for D B I can see that I'm working with this
3:59
my database here but if I go for the DBS show DBS I'm finding that my
4:07
database is not coming in the list because this my database is having no collection so let us insert one collection here
4:15
under this my database okay so in the case what we shall do we shall create a
4:20
collection whose size is 5 mb and it can store 100 documents maximum so how
4:27
to do that one so db. create create collection the name of the collection is
4:40
name of the document is employee this the collection is there so here the name of the
4:46
collection is employee and then we shall go for the size so as I have mentioned
4:52
it will be 5 mb so 5 to 4 to 880 and if it will have
#Computers & Electronics
#Programming
#Software