1
00:00:00,000 --> 00:00:05,062
[MUSIC]

2
00:00:05,062 --> 00:00:10,481
Now that we have had a brief introduction
to MongoDB, it's time to get hold

3
00:00:10,481 --> 00:00:16,430
of MongoDB, install it on our computer,
and then interact with MongoDB server.

4
00:00:17,600 --> 00:00:23,003
To get started, of course, the first thing
is to install MongoDB on your computer

5
00:00:24,479 --> 00:00:29,970
To install MongoDB on your computer,
go to mongodb.com.

6
00:00:29,970 --> 00:00:33,850
And on the right hand side here,
you will see the Download button.

7
00:00:33,850 --> 00:00:36,760
You can click on the Download
button in order to

8
00:00:36,760 --> 00:00:39,600
obtain the links to that download.

9
00:00:39,600 --> 00:00:43,885
So when you click on the Download button,
you'll be taken to the Download Center,

10
00:00:43,885 --> 00:00:49,480
where you would see various versions
of MongoDB available to us.

11
00:00:49,480 --> 00:00:53,750
You can access it online,
using database as a service.

12
00:00:53,750 --> 00:00:59,710
But in our case, we're going to download
MongoDB, and install it on our computer.

13
00:00:59,710 --> 00:01:04,870
So to do that, we will choose
the Community Server version of MongoDB.

14
00:01:04,870 --> 00:01:09,651
And when you do that, you will be
automatically given access to the specific

15
00:01:09,651 --> 00:01:12,502
version suited for your computer platform.

16
00:01:12,502 --> 00:01:17,968
So in this case, I'm going to download
the OSX version of the MongoDB and

17
00:01:17,968 --> 00:01:20,300
then do the installation.

18
00:01:20,300 --> 00:01:25,990
If you are using a Windows machine then
you can download the Windows installer.

19
00:01:25,990 --> 00:01:28,210
For a Linux machine correspondingly,

20
00:01:28,210 --> 00:01:31,880
you can download
the corresponding installer here.

21
00:01:31,880 --> 00:01:33,380
To install MongoDB,

22
00:01:34,950 --> 00:01:40,670
the MongoDB documentation itself
provides you with detailed instructions.

23
00:01:40,670 --> 00:01:43,910
I don't think I can give you any
better instructions than what

24
00:01:43,910 --> 00:01:45,440
is already provided there.

25
00:01:45,440 --> 00:01:51,530
So to understand how to install
MongoDB on your computer,

26
00:01:51,530 --> 00:01:55,430
head over to
docs.mongodb.com/manual/installation.

27
00:01:55,430 --> 00:02:00,600
And then click on
the Install MongoDB Community Edition.

28
00:02:02,460 --> 00:02:07,910
And here, you will see the installation
instructions for various platforms.

29
00:02:07,910 --> 00:02:11,650
So to install on Windows,
click on the Install on Windows and

30
00:02:11,650 --> 00:02:14,500
then go through the steps given here.

31
00:02:14,500 --> 00:02:19,600
And they specify how to
download MongoDB and

32
00:02:19,600 --> 00:02:25,320
then install the MongoDB Community Edition
on your computer.

33
00:02:25,320 --> 00:02:31,040
So, go ahead and follow these instructions
to install MongoDB on your computer.

34
00:02:32,180 --> 00:02:37,440
And also they tell you how
to get started with MongoDB.

35
00:02:37,440 --> 00:02:39,830
Now once you have
completed the installation,

36
00:02:39,830 --> 00:02:45,110
you can proceed ahead to the next
step that we gave in the exercise.

37
00:02:45,110 --> 00:02:48,250
Similarly, if you are installing in OSX,

38
00:02:48,250 --> 00:02:53,080
just follow instructions given
here to install it on OSX machine.

39
00:02:53,080 --> 00:02:58,950
And also correspondingly for
the Linux platforms.

40
00:02:58,950 --> 00:03:03,230
I assume by now you have installed
MongoDB following the instructions

41
00:03:03,230 --> 00:03:05,790
given in the documentation.

42
00:03:05,790 --> 00:03:10,430
Then, once you have completed that go to
a convenient location on your computer.

43
00:03:10,430 --> 00:03:12,378
So here I am in my Coursera folder.

44
00:03:12,378 --> 00:03:19,192
I'm going to create a new
folder here named, mongodb,

45
00:03:19,192 --> 00:03:26,400
and then inside the mongodb folder I
will create a new folder named data.

46
00:03:26,400 --> 00:03:31,630
Now this data folder is where
mongodb will install all the data

47
00:03:31,630 --> 00:03:34,890
relevant for my current project.

48
00:03:34,890 --> 00:03:39,450
Once you have created this folders,
open a command window or

49
00:03:39,450 --> 00:03:44,610
a Terminal at the mongodb folder.

50
00:03:44,610 --> 00:03:47,239
So here, I have my Terminal, and

51
00:03:47,239 --> 00:03:51,080
I am currently in the mongodb
folder that I have just created.

52
00:03:51,080 --> 00:03:57,714
So at the prompt in order
to start the Mongo server,

53
00:03:57,714 --> 00:04:01,727
type mongod --dbpath =data

54
00:04:01,727 --> 00:04:06,678
--bind_ip 127.0.0.1.

55
00:04:06,678 --> 00:04:10,880
Recall that we already have the data

56
00:04:10,880 --> 00:04:15,788
subfolder inside the mongodb folder.

57
00:04:15,788 --> 00:04:20,670
So the dbpath here essentially
takes the path to the folder,

58
00:04:20,670 --> 00:04:23,870
which will store the data for
my Mongo application.

59
00:04:23,870 --> 00:04:31,300
Now, you can execute this mongod command
from any location on your computer.

60
00:04:31,300 --> 00:04:36,026
As long as you specify the complete
path to the location of

61
00:04:36,026 --> 00:04:41,250
the data folder where your MongoDB
data is going to be stored.

62
00:04:41,250 --> 00:04:44,230
Since I am already in the mongodb folder,
and

63
00:04:44,230 --> 00:04:48,310
the data folder is a sub-folder
of the mongodb folder,

64
00:04:48,310 --> 00:04:54,780
I can just simply execute the command
by saying mongodb -- dbpath=data.

65
00:04:54,780 --> 00:04:57,860
And this should start
up your MongoDB here.

66
00:04:57,860 --> 00:05:02,430
And if it asks for some permissions,
just allow MongoDB to be

67
00:05:03,510 --> 00:05:08,280
available for being connected
to from different locations.

68
00:05:08,280 --> 00:05:15,148
Open another Terminal window or command
prompt and add the prompt, type mongo,

69
00:05:15,148 --> 00:05:21,017
in order to start the Mongo report or
Mongo read evaluate print loop.

70
00:05:21,017 --> 00:05:25,910
So this is a command line way
of accessing your Mongo server.

71
00:05:25,910 --> 00:05:30,263
So when you type Mongo,
it will automatically connect itself to

72
00:05:30,263 --> 00:05:35,921
the MongoDB server that is running on
your computer at the port number 27017.

73
00:05:35,921 --> 00:05:39,230
So here,
you see this is automatically connecting.

74
00:05:39,230 --> 00:05:43,930
So when you start the Mongo Ripple,
it will know exactly where to look for

75
00:05:43,930 --> 00:05:49,200
the default MongoDB server which will
be running at port number 27017.

76
00:05:49,200 --> 00:05:52,700
In this course, we're going to be running
the MongoDB server at its default

77
00:05:52,700 --> 00:05:57,060
port number and so
that it's easy to access data.

78
00:05:57,060 --> 00:06:00,780
Also on OSX machine,

79
00:06:00,780 --> 00:06:06,410
you might see a warning like this
which says soft rlimits too low.

80
00:06:06,410 --> 00:06:11,290
You can just ignore that warning
everything will work just fine even though

81
00:06:11,290 --> 00:06:14,220
you will see that warning
being printed there.

82
00:06:14,220 --> 00:06:20,181
This has something to do with the amount
of memory space available for

83
00:06:20,181 --> 00:06:22,047
your Mongo process.

84
00:06:22,047 --> 00:06:24,467
And this will be just fine to run for

85
00:06:24,467 --> 00:06:30,390
the development version of the Mongo that
we're going to be using in this course.

86
00:06:30,390 --> 00:06:35,310
Now once the Mongo Ripple starts,
you will see a prompt, and at the prompt,

87
00:06:35,310 --> 00:06:40,870
you can type various commands
to access your Mongo server.

88
00:06:40,870 --> 00:06:45,720
So at the prompt, if you type db,
it will specify the specific

89
00:06:45,720 --> 00:06:50,650
database on the Mongo server to
which it is currently connected.

90
00:06:50,650 --> 00:06:55,830
So, now we're going to create a new
database there with the name confusion.

91
00:06:55,830 --> 00:07:01,800
So to create a new database, I would
just say, use conFusion at the prompt.

92
00:07:01,800 --> 00:07:04,420
So this will automatically create

93
00:07:04,420 --> 00:07:09,500
a new database named conFusion
if it doesn't already exist.

94
00:07:09,500 --> 00:07:13,965
If it exists then it will automatically
switch to that conFusion database,

95
00:07:13,965 --> 00:07:20,480
there after any commands that you execute
will be accessing this conFusion database.

96
00:07:20,480 --> 00:07:26,425
So at the prompt if you now type db you
will see it prints out saying conFusion.

97
00:07:26,425 --> 00:07:31,385
The test that it printed earlier with the
test database that is automatically set up

98
00:07:31,385 --> 00:07:34,085
when you start running the Mongo server.

99
00:07:34,085 --> 00:07:38,255
Now, to learn the various
commands that are supported

100
00:07:38,255 --> 00:07:42,175
you can just type db.help() at the prompt.

101
00:07:42,175 --> 00:07:46,425
And this will print out a set of
commands that are available to you

102
00:07:47,540 --> 00:07:51,040
at the Mongo Ripple command prompt.

103
00:07:51,040 --> 00:07:56,660
We're going to be using a few of
these to access the MongoDB database.

104
00:07:56,660 --> 00:08:01,120
Again, typing db you see that we
are accessing the conFusion database.

105
00:08:01,120 --> 00:08:06,400
Let's create a new collection
in this conFusion database.

106
00:08:06,400 --> 00:08:11,280
To do that, at the prompt type db.dishes.

107
00:08:11,280 --> 00:08:15,090
Now the dishes here
refers to a collection.

108
00:08:15,090 --> 00:08:17,030
Now if this collection already exists,

109
00:08:17,030 --> 00:08:19,840
then this will access
the dishes collection.

110
00:08:19,840 --> 00:08:24,250
If it doesn't exist, then it'll create
the dishes collection at this point.

111
00:08:24,250 --> 00:08:27,780
So in the dishes collection
I'm going to insert.

112
00:08:27,780 --> 00:08:30,920
So this is where we use the insert

113
00:08:30,920 --> 00:08:34,490
function that is available
on the collection here.

114
00:08:34,490 --> 00:08:39,348
And then let's insert a record
into this collection,

115
00:08:39,348 --> 00:08:42,660
or a document into this collection.

116
00:08:42,660 --> 00:08:47,899
So at the prompt I'll type name,

117
00:09:01,238 --> 00:09:06,009
I'm just typing some random stuff
here just to show you that we

118
00:09:06,009 --> 00:09:10,000
can insert a document into the database.

119
00:09:10,000 --> 00:09:14,570
So with this, this particular
JSON document that you see here,

120
00:09:14,570 --> 00:09:17,890
will be automatically
inserted into the database.

121
00:09:17,890 --> 00:09:23,150
So the insert function
takes a JSON document

122
00:09:23,150 --> 00:09:27,080
as its parameter here.

123
00:09:27,080 --> 00:09:31,100
And immediately the Mongo Ripple will

124
00:09:31,100 --> 00:09:36,040
respond saying WriteResult and
number inserted is 1.

125
00:09:36,040 --> 00:09:37,920
So one record has been inserted,

126
00:09:37,920 --> 00:09:41,370
one document has been inserted
into this collection.

127
00:09:41,370 --> 00:09:48,372
Now to print out the various
documents in this collection,

128
00:09:48,372 --> 00:09:53,421
we can simply say db.dishes.find().

129
00:09:53,421 --> 00:09:57,711
So the find function will find all
the documents in this collection and

130
00:09:57,711 --> 00:10:00,030
it will print out those documents.

131
00:10:00,030 --> 00:10:06,779
Now, if I just say find, then it will
print out this information like this here,

132
00:10:06,779 --> 00:10:11,964
as a single line and you can see
that this is a JSON string here.

133
00:10:11,964 --> 00:10:14,231
Now we can also use the,

134
00:10:17,762 --> 00:10:22,602
Pretty function that is available
which will print out the documents

135
00:10:22,602 --> 00:10:25,430
in a more easily readable manner.

136
00:10:25,430 --> 00:10:29,730
So if you type db.dishes.find().pretty(),
so

137
00:10:29,730 --> 00:10:35,490
you see that the document that we had
inserted earlier Is now seen here.

138
00:10:35,490 --> 00:10:37,580
So you can see that the name and

139
00:10:37,580 --> 00:10:42,810
the description that we had inserted
is available in this document.

140
00:10:42,810 --> 00:10:43,860
In addition,

141
00:10:43,860 --> 00:10:49,520
note how MongoDB has automatically
added an id field to this document.

142
00:10:49,520 --> 00:10:53,582
So if you don't explicitly
specify the id field MongoDB will

143
00:10:53,582 --> 00:10:55,818
automatically add the id field.

144
00:10:55,818 --> 00:11:02,653
And as explained in the lecture earlier,
this is of the type object id and

145
00:11:02,653 --> 00:11:09,810
you can see that this string specifies
the ObjectId information there.

146
00:11:09,810 --> 00:11:14,550
Now this ObjectId as we learnt
in the lecture earlier,

147
00:11:14,550 --> 00:11:18,905
has a specific fall back which
it also includes the timestamp,

148
00:11:18,905 --> 00:11:23,240
the machine name, the process ID and

149
00:11:23,240 --> 00:11:28,030
also the increment value
that is stored here.

150
00:11:28,030 --> 00:11:30,940
So now, similarly if you want to,

151
00:11:30,940 --> 00:11:35,810
I can simply create an object
ID using the Mongo Ripple.

152
00:11:35,810 --> 00:11:39,794
So let me just type var id = new,

153
00:11:44,118 --> 00:11:48,346
ObjectId(); just to
demonstrate to you that

154
00:11:48,346 --> 00:11:52,180
the ObjectId will contain the timestamp.

155
00:11:52,180 --> 00:11:57,234
So here I have created this variable
them ID which is a new ObjectId.

156
00:11:57,234 --> 00:12:02,037
Now I can simply type
id.getTimestamp(); and

157
00:12:02,037 --> 00:12:06,953
you will notice that this
will print out the current

158
00:12:06,953 --> 00:12:11,195
time value in the ISO
format as you see here.

159
00:12:13,275 --> 00:12:17,493
So this shows you that the ObjectId
actually contains the timestamp

160
00:12:17,493 --> 00:12:18,530
field here.

161
00:12:18,530 --> 00:12:23,602
So similarly, you ask for
the other fields also from the ObjectId.

162
00:12:23,602 --> 00:12:28,328
And to quit the Mongo Ripple
just type exit at the prompt,

163
00:12:28,328 --> 00:12:31,281
and the Mongo Ripple will exit, and

164
00:12:31,281 --> 00:12:37,520
then bring you back to your Terminal
prompt or your command prompt.

165
00:12:37,520 --> 00:12:40,310
With this, we complete this exercise.

166
00:12:40,310 --> 00:12:43,520
In this exercise,
we have seen how we download and

167
00:12:43,520 --> 00:12:45,860
install MongoDB on our computer.

168
00:12:45,860 --> 00:12:50,819
How we start up a MongoDB server,
and then access the MongoDB server

169
00:12:50,819 --> 00:12:54,740
using the Mongo Ripple and
interact with the server.

170
00:12:54,740 --> 00:13:00,886
[MUSIC]