Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,510 --> 00:00:07,020
All right now that you've got MongoDB installed on your local system the next step is to go ahead
2
00:00:07,110 --> 00:00:07,970
and use it
3
00:00:08,010 --> 00:00:08,550
right?
4
00:00:08,550 --> 00:00:14,730
So let's head over to the www.mongodb.com website and we're going to navigate to the documentation
5
00:00:15,180 --> 00:00:19,100
through the docs tab and then we're going to head over to getting started.
6
00:00:19,320 --> 00:00:25,700
And if you take a look over here, there's the MongoDB CRUD operations.
7
00:00:25,770 --> 00:00:31,680
I recommend that whenever you're exploring a new database system the first thing to wrap your head around
8
00:00:31,770 --> 00:00:36,090
is how do you perform CRUD using that particular database.
9
00:00:36,090 --> 00:00:43,680
So we saw how we can create, read, update and delete data using a SQL based system such as SQL lite
10
00:00:43,950 --> 00:00:46,960
in the last module. In this section
11
00:00:46,980 --> 00:00:52,740
we're going to look at how we can do that using MongoDB on the command line.
12
00:00:52,770 --> 00:01:00,690
So this is the time to open up your hyperterminal. And after the prompt I'm going to type mongod or
13
00:01:00,690 --> 00:01:05,019
some people call it 'mongod', and this will spin up our mongo server
14
00:01:05,069 --> 00:01:13,570
when you hit enter. Once you see waiting for connections on port 27017 then you are ready to go.
15
00:01:13,650 --> 00:01:16,700
This is kind of similar to how we had a localhost 3000.
16
00:01:16,800 --> 00:01:22,600
But in this case we've got a local database that's being served up on this particular port.
17
00:01:22,860 --> 00:01:30,960
So now if you're go into Shell and new tab or if take a look here at the shortcut which on Mac is COMAND +
18
00:01:30,990 --> 00:01:37,960
T, then we can open up a brand new terminal. And this is a different connection because if you notice
19
00:01:37,960 --> 00:01:41,940
him where on MongoDB database is running
20
00:01:41,980 --> 00:01:43,400
we actually don't see our prompt.
21
00:01:43,420 --> 00:01:49,750
We can't type something in here and we can't for example cd over to the desktop over here because we
22
00:01:49,750 --> 00:01:52,470
don't have an active connection to our terminal.
23
00:01:52,540 --> 00:01:55,810
This terminal is connected to the MongoDB database.
24
00:01:56,170 --> 00:02:04,840
In our new terminal however we do now have a prompt and we can now tap into the Mongo shell. And to do
25
00:02:04,840 --> 00:02:05,110
that
26
00:02:05,110 --> 00:02:08,870
the key word is mongo without the 'd'.
27
00:02:08,979 --> 00:02:16,030
And now if we hit enter and you see your prompt being changed to this > then you are ready
28
00:02:16,060 --> 00:02:18,880
to use the Mongo shell.
29
00:02:18,950 --> 00:02:25,010
So the Mongo shell is basically just a way for us to be able to interact with our MongoDB databases on our
30
00:02:25,010 --> 00:02:28,260
local system using the command line.
31
00:02:28,550 --> 00:02:34,760
But before we create a brand new database, it's good practice to take a look at what databases we already
32
00:02:34,760 --> 00:02:36,540
have. How do we do that?
33
00:02:36,590 --> 00:02:41,150
Well if at any point you get stuck and you're inside the Mongo shell
34
00:02:41,150 --> 00:02:48,020
so you've got your little angle bracket showing there, you can always type help, as a cry for help, and
35
00:02:48,110 --> 00:02:53,810
you will be able to get a helpful list of things that you can do while using the Mongo shell.
36
00:02:53,810 --> 00:02:59,930
So the one that I want to highlight to you is this one which is the command show dbs. And this shows
37
00:02:59,930 --> 00:03:04,910
the names of the database that we currently have on our local system.
38
00:03:04,910 --> 00:03:12,980
So let's go ahead and try that out. Show dbs. Hit enter. And you can see that by default when you install
39
00:03:12,980 --> 00:03:21,170
MongoDB, it comes preloaded with three databases: admin, config and local and you can see that all
40
00:03:21,170 --> 00:03:26,560
of them are taking up zero gigabytes of space because they're completely empty.
41
00:03:26,570 --> 00:03:31,840
This is what it would look like if you have a brand new configuration of MongoDB.
42
00:03:31,910 --> 00:03:41,470
So let's go ahead and create our very first database. And to do that all we have to do is type use
43
00:03:41,620 --> 00:03:44,980
and then we specify the name of our database.
44
00:03:45,100 --> 00:03:52,060
So let's say the I wanted to create a database again for my shop so I might call my database shopDB.
45
00:03:52,540 --> 00:03:57,400
I can write use shopDB and hit enter.
46
00:03:57,400 --> 00:04:03,240
So now Mongo tells me that I've switched to the database that is called ShopDB.
47
00:04:03,250 --> 00:04:09,700
But if I go back and I write show dbs, I don't see shopDB anywhere.
48
00:04:10,120 --> 00:04:14,560
Well that's because for it to be listed, it has to have some content.
49
00:04:14,560 --> 00:04:16,490
So we first have to give it some data.
50
00:04:16,720 --> 00:04:24,450
We're now ready to tackle the 'C' in CRUD and see how we can create data using MongoDB.
51
00:04:24,690 --> 00:04:31,500
If you ever want to know which database you're currently in, you can always type db hit enter and it
52
00:04:31,500 --> 00:04:35,230
tells you that you're currently working within the shop database.
53
00:04:35,610 --> 00:04:38,690
So how do we create data in our database?
54
00:04:38,700 --> 00:04:45,090
This is the time to head back over to our documentation where we've got the MongoDB CRUD operations
55
00:04:45,120 --> 00:04:46,410
explained to us.
56
00:04:46,410 --> 00:04:54,300
So what we want to do is to create and you can see that in order to create one entry or many entries
57
00:04:54,360 --> 00:04:58,520
into our database, the syntax looks something like this.
58
00:04:58,620 --> 00:05:05,260
We write db.collection and this collection is actually the name of a collection.
59
00:05:05,310 --> 00:05:11,820
And if that collection name doesn't currently exist then by writing this line and by executing it, it
60
00:05:11,820 --> 00:05:18,920
will actually create that collection. And then we use a method such as insertOne or insertMany.
61
00:05:19,380 --> 00:05:22,750
And this is an example of how you would use this method.
62
00:05:23,010 --> 00:05:30,000
So for example if you wanted a collection called users inside your database you would say db.users.
63
00:05:30,090 --> 00:05:31,610
insertOne.
64
00:05:31,860 --> 00:05:39,250
And inside the method insertone you would pass over a Javascript object which will be the document.
65
00:05:39,750 --> 00:05:42,840
So it will have fields and it will have values.
66
00:05:43,260 --> 00:05:49,590
And this pretty much follows the key value pairs that we've been seeing in Javascript objects.
67
00:05:49,660 --> 00:05:54,120
Let's take what we learn here and let's implement it for our shopDB.
68
00:05:54,250 --> 00:06:01,810
So let's say db. and the collection that we want to make is the one that's called products. Collections
69
00:06:01,960 --> 00:06:07,630
in MongoDB is kind of similar to tables in the SQL world.
70
00:06:07,720 --> 00:06:14,350
They are a collection of documents and a document is simply just a single data record,
71
00:06:14,350 --> 00:06:16,080
so that would be a single row
72
00:06:16,150 --> 00:06:22,660
if you were using a SQL based database. If this collection products doesn't currently exist in our
73
00:06:22,660 --> 00:06:31,300
database then by executing this line of code it will create that collection called products. And then
74
00:06:31,300 --> 00:06:40,030
we write another . and we call the method insertOne and we open up a set of round brackets or parentheses.
75
00:06:40,660 --> 00:06:42,950
As the inputs for this method
76
00:06:42,980 --> 00:06:53,140
insertOne we get to insert a Javascript object. The product that we want to insert is the pen product
77
00:06:53,140 --> 00:06:55,110
that we had previously.
78
00:06:55,120 --> 00:06:59,310
So in this case it will have an id of 1,
79
00:06:59,680 --> 00:07:09,550
it will have a name of let's say Pen and it will have a price of 1.20.
80
00:07:09,550 --> 00:07:17,810
This is all the data that I want to add to each record or each document inside my product's collection.
81
00:07:18,670 --> 00:07:27,510
And when I hit enter my data gets added to my database. And now I can use the key words show collections
82
00:07:28,690 --> 00:07:34,390
and it will show me all the collections that currently exist in the current database which remember
83
00:07:34,390 --> 00:07:35,620
is shopDB.
84
00:07:35,620 --> 00:07:42,970
So I've only got one collection which is called products. And in my products I've only got one entry
85
00:07:43,210 --> 00:07:46,010
which is my pen product.
86
00:07:46,210 --> 00:07:52,170
This is how easy it is to add data to our database using MongoDB.
87
00:07:52,340 --> 00:07:59,050
And if you look at the documentation you can see that you can either insertOne or you can insertMany.
88
00:07:59,180 --> 00:08:05,470
And if you click on it you can see the syntax as well as examples for each of these methods.
89
00:08:07,250 --> 00:08:14,270
MongoDB is extremely well documented and pretty much anything you want to do with it is explained really
90
00:08:14,270 --> 00:08:19,150
well through the use of graphics and examples in their documentation.
91
00:08:19,160 --> 00:08:26,810
So now it's your turn to create a new document using the Mongo shell and you're going to create the
92
00:08:26,810 --> 00:08:31,920
second product in our products collection which is the pencil product.
93
00:08:31,970 --> 00:08:33,280
Pause the video now
94
00:08:33,380 --> 00:08:39,360
and in your Mongo shell create the second product.
95
00:08:39,419 --> 00:08:42,950
OK so we're going to follow the format that we had previously.
96
00:08:42,990 --> 00:08:45,150
We're going to tap into our database,
97
00:08:45,150 --> 00:08:51,960
we're going to look for the collection that's called products and then we're going to again insert one
98
00:08:52,020 --> 00:09:01,890
product. And that product is going to be again a Javascript object and it's going to have an id of 2
99
00:09:02,070 --> 00:09:04,830
this time because this is the second entry.
100
00:09:05,160 --> 00:09:13,830
And then it's going to have a name of pencil and it will have a price of 0.80, so 80 cents or
101
00:09:13,890 --> 00:09:15,000
80p.
102
00:09:15,210 --> 00:09:20,260
And when we hit enter, then we've created our second document.
103
00:09:21,000 --> 00:09:27,790
So now we have a database called ShopDB that contains a single collection called products
104
00:09:27,900 --> 00:09:35,250
and inside this collection is two documents one with this data and another with this data.
11476
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.