WEBVTT

0:00:09.540000 --> 0:00:19.500000
 All right. Now what if we wanted to
 identify or check how many and I'll

0:00:19.500000 --> 0:00:22.980000
 go back to the database here,
 I'll list all the databases.

0:00:22.980000 --> 0:00:29.980000
 Let's utilize the city database here.

0:00:29.980000 --> 0:00:32.440000
 There we are. We have city there.

0:00:32.440000 --> 0:00:37.320000
 So we can say db.city,
 but we can say db.find.

0:00:37.320000 --> 0:00:45.120000
 db.city.find to list out
 the documents here.

0:00:45.120000 --> 0:00:48.100000
 You can see these are all the
 actual records of documents.

0:00:48.100000 --> 0:00:49.980000
 So we have multiple fields.

0:00:49.980000 --> 0:00:55.260000
 We have the ID, the city, the location,
 which points to latitude and longitude

0:00:55.260000 --> 0:01:00.400000
 coordinates and then population
 and the state.

0:01:00.400000 --> 0:01:06.240000
 All right. So what if we wanted to find
 how many of these documents, and

0:01:06.240000 --> 0:01:09.380000
 I'm referring to the number, how many
 of these documents all have the

0:01:09.380000 --> 0:01:15.120000
 state of, let's say, in this case, MA,
 which I'm assuming is Massachusetts,

0:01:15.120000 --> 0:01:16.760000
 I might be wrong, but
 let's try and do that.

0:01:16.760000 --> 0:01:23.040000
 The way we would do this, we would
 say, is city db.city.find and then

0:01:23.040000 --> 0:01:25.600000
 in here we would put in our
 parameters or criteria.

0:01:25.600000 --> 0:01:29.040000
 So the field in this case is city.

0:01:29.040000 --> 0:01:34.020000
 And then we would say that's going
 to be equal to MA or just MA, as it

0:01:34.020000 --> 0:01:38.300000
 were, and we'll close the curly braces
 and then the count, right?

0:01:38.300000 --> 0:01:45.580000
 So that'll tell us how many documents
 here essentially meet that criteria.

0:01:45.580000 --> 0:01:52.800000
 We'll hit zero. So essentially what we
 did is we found out how many cities

0:01:52.800000 --> 0:01:59.300000
 of the state Massachusetts are present
 in the collection city, in the

0:01:59.300000 --> 0:02:00.600000
 database city, right?

0:02:00.600000 --> 0:02:03.860000
 So a little bit abstract, but
 that's how that would work.

0:02:03.860000 --> 0:02:07.920000
 And you can, again, utilize any field
 to perform your queries there.

0:02:07.920000 --> 0:02:13.480000
 We could also do this to, for example,
 the population field seems interesting.

0:02:13.480000 --> 0:02:19.080000
 And this is where I wanted to show you
 the logical operators in the Mongo

0:02:19.080000 --> 0:02:23.500000
 query language. So for example, we can
 try and find out how many cities

0:02:23.500000 --> 0:02:28.140000
 have a population greater than, let's
 say, 15,000 in the collection city

0:02:28.140000 --> 0:02:29.460000
 in the database city.

0:02:29.460000 --> 0:02:31.920000
 To do that, we would say use city.

0:02:31.920000 --> 0:02:36.360000
 We currently are using that database.

0:02:36.360000 --> 0:02:39.860000
 And now if we say show collections,
 we only have one collection called

0:02:39.860000 --> 0:02:45.560000
 city. Again, we can say db.city.find
 and we can then utilize the field

0:02:45.560000 --> 0:02:50.340000
 population. So it needs to
 match exactly population.

0:02:50.340000 --> 0:02:55.080000
 And then we say in here, when we utilize
 an logical operation, we can

0:02:55.080000 --> 0:02:57.540000
 utilize the dollar symbol.

0:02:57.540000 --> 0:03:00.980000
 And we say GT, which stands
 for greater than.

0:03:00.980000 --> 0:03:05.920000
 There's also not equal to or equal
 to, which I'll also show you in the

0:03:05.920000 --> 0:03:11.760000
 next video and we'll then
 say the value here.

0:03:11.760000 --> 0:03:18.200000
 So 15,000. And yeah, so we'll close
 this curly brace, the original one

0:03:18.200000 --> 0:03:21.080000
 and then the actual bracket there.

0:03:21.080000 --> 0:03:26.900000
 So essentially saying can you find,
 can you find how many cities have

0:03:26.900000 --> 0:03:30.920000
 a population greater than 15,000 in
 the collection city in the database

0:03:30.920000 --> 0:03:33.820000
 city? And then we say count this.

0:03:33.820000 --> 0:03:35.660000
 Can you tell us the total number here?

0:03:35.660000 --> 0:03:39.120000
 So 5,785 cities.

0:03:39.120000 --> 0:03:42.420000
 And you know, we can change this so
 we can say how many cities have a

0:03:42.420000 --> 0:03:50.620000
 population greater than let's say
 100,000, only four of them.

0:03:50.620000 --> 0:03:54.480000
 All right. So that's how this works.

0:03:54.480000 --> 0:03:57.720000
 Now if we get rid of count, it'll
 actually show us those cities.

0:03:57.720000 --> 0:04:01.780000
 So in this case, New York, Brooklyn,
 Chicago, as you can see here, which

0:04:01.780000 --> 0:04:06.600000
 is pretty cool. So you're starting to get
 a feel as to why some web applications

0:04:06.600000 --> 0:04:10.380000
 need this type of databases opposed
 to a relational database.

0:04:10.380000 --> 0:04:15.760000
 So it all comes down to, you know, what
 type of data you're going to be

0:04:15.760000 --> 0:04:19.180000
 storing and how fast you
 need to access it, right?

0:04:19.180000 --> 0:04:21.580000
 Now we can also run some advanced ones.

0:04:21.580000 --> 0:04:25.860000
 So for example, we can say how many
 cities in the state of let's say,

0:04:25.860000 --> 0:04:33.120000
 let's say New York have a, you know,
 maybe we can say population, but

0:04:33.120000 --> 0:04:34.300000
 let's try a different one.

0:04:34.300000 --> 0:04:40.680000
 So we can say DB dot city dot find.

0:04:40.680000 --> 0:04:44.100000
 Let's see what other states we have here.


0:04:44.100000 --> 0:04:50.620000
 So we have, we need to type
 in it to display the others.

0:04:50.620000 --> 0:04:55.900000
 So it we're pretty much seeing
 just Massachusetts.

0:04:55.900000 --> 0:05:05.080000
 Let's see, Massachusetts still,
 we can say DB dot city dot find.

0:05:05.080000 --> 0:05:10.000000
 Let's see if we can find whether
 there's other states here.

0:05:10.000000 --> 0:05:16.760000
 So I'm going to say in here,
 I will just put in state.

0:05:16.760000 --> 0:05:22.100000
 And I'll say the state equals
 something like FL for Florida.

0:05:22.100000 --> 0:05:26.020000
 And we'll close that up there
 and let's hit enter.

0:05:26.020000 --> 0:05:28.300000
 So there we are Florida as an example.

0:05:28.300000 --> 0:05:31.400000
 So we can say DB dot city dot find.

0:05:31.400000 --> 0:05:37.480000
 And in here now coming back to utilizing
 a different or a logical operator

0:05:37.480000 --> 0:05:41.400000
 to use the logical operator like we
 did in the structured query language

0:05:41.400000 --> 0:05:44.680000
 instead of saying and or typing it
 out, we use the dollar symbol.

0:05:44.680000 --> 0:05:51.500000
 And then we say and and then we use a
 colon and then specify our set here.

0:05:51.500000 --> 0:05:58.280000
 So we're saying and we want to find
 the population greater than.

0:05:58.280000 --> 0:06:03.220000
 So we're going to say within this
 set, we'll just say population.

0:06:03.220000 --> 0:06:05.900000
 That's going to be greater than.

0:06:05.900000 --> 0:06:08.220000
 So GT and then the value.

0:06:08.220000 --> 0:06:14.600000
 So let's say, you know, 15,000, for
 example, and we'll close those two

0:06:14.600000 --> 0:06:15.840000
 curly braces there.

0:06:15.840000 --> 0:06:19.380000
 And then the second operation,
 which is going to be the state.

0:06:19.380000 --> 0:06:23.720000
 So the second match, in this case,
 let's say the state is Florida.

0:06:23.720000 --> 0:06:25.920000
 So FL for Florida.

0:06:25.920000 --> 0:06:30.080000
 And we'll close the curly braces there.

0:06:30.080000 --> 0:06:35.260000
 And we then need to close the single
 bracket or the standard bracket and

0:06:35.260000 --> 0:06:36.880000
 then normal bracket.

0:06:36.880000 --> 0:06:39.340000
 And then we say count, right?

0:06:39.340000 --> 0:06:43.480000
 Hit enter. So you can see this
 is a pretty advanced query.

0:06:43.480000 --> 0:06:45.920000
 We're looking for very specific
 information here.

0:06:45.920000 --> 0:06:49.700000
 And let's see where that can
 give us that information.

0:06:49.700000 --> 0:06:54.120000
 So we're asking it to tell
 us DB dot city dot find.

0:06:54.120000 --> 0:07:02.180000
 Can you please tell us the or run the
 operation where the first option

0:07:02.180000 --> 0:07:08.820000
 is the population needs to
 be greater than 15,000.

0:07:08.820000 --> 0:07:16.800000
 Secondly, it needs to essentially tell
 us the how many cities in the state

0:07:16.800000 --> 0:07:21.840000
 of Florida have a population greater
 than 15,000 in the collection city

0:07:21.840000 --> 0:07:23.360000
 and the database city.

0:07:23.360000 --> 0:07:24.660000
 So I'll let enter.

0:07:24.660000 --> 0:07:25.860000
 This is taking a while.

0:07:25.860000 --> 0:07:28.460000
 I don't think it actually
 we actually got a result.

0:07:28.460000 --> 0:07:31.440000
 So let's try a different state here.

0:07:31.440000 --> 0:07:34.280000
 That operation did not run successfully.

0:07:34.280000 --> 0:07:36.400000
 So let's try and copy this here.

0:07:36.400000 --> 0:07:39.960000
 I was going to copy this
 and paste that in there.

0:07:39.960000 --> 0:07:46.440000
 We can change this to maybe let's say
 New York, for example, hit enter.

0:07:46.440000 --> 0:07:50.240000
 So for some reason that's
 not displaying that.

0:07:50.240000 --> 0:07:51.960000
 Let's try a different one.

0:07:51.960000 --> 0:07:54.760000
 One that might work here.

0:07:54.760000 --> 0:08:00.880000
 Actually, what we could do is if we try
 and let me just paste in the query.

0:08:00.880000 --> 0:08:02.440000
 So actually hold on.

0:08:02.440000 --> 0:08:04.200000
 Let me just terminate that there.

0:08:04.200000 --> 0:08:11.800000
 Let's go back into Mongo show DBS and
 we'll say use city and then we say

0:08:11.800000 --> 0:08:13.400000
 show collections.

0:08:13.400000 --> 0:08:15.620000
 I'm going to use that same query here.

0:08:15.620000 --> 0:08:19.380000
 So if I run that again, let's see
 if we get any result from this.

0:08:19.380000 --> 0:08:23.160000
 We may need to change the greater than
 here because they might not be

0:08:23.160000 --> 0:08:27.900000
 city. Well, I assume they are, but
 they only might be a few documents

0:08:27.900000 --> 0:08:34.140000
 with the state of Florida or that have
 the field of state that's equal

0:08:34.140000 --> 0:08:36.660000
 to Florida or FL as it were.

0:08:36.660000 --> 0:08:41.660000
 So we'll just give it a few seconds here.


0:08:41.660000 --> 0:08:43.260000
 Let's try a different one.

0:08:43.260000 --> 0:08:45.620000
 Maybe something like Indiana.

0:08:45.620000 --> 0:08:51.340000
 So IN or we can try MA here
 as we did Massachusetts.

0:08:51.340000 --> 0:08:55.160000
 That actually should work because we're
 just checking population and then

0:08:55.160000 --> 0:08:58.740000
 greater than 15,000.

0:08:58.740000 --> 0:09:00.800000
 That should work.

0:09:00.800000 --> 0:09:06.220000
 And then the second operation is state,
 which is Massachusetts here.

0:09:06.220000 --> 0:09:09.820000
 And oh, there we are.

0:09:09.820000 --> 0:09:12.680000
 I think I found out why
 my query wasn't working.

0:09:12.680000 --> 0:09:13.880000
 That's because of my syntax.

0:09:13.880000 --> 0:09:18.440000
 I forgot to close the actual
 curly braces here.

0:09:18.440000 --> 0:09:19.820000
 So we hit enter now.

0:09:19.820000 --> 0:09:20.600000
 There we are. 378.

0:09:20.600000 --> 0:09:22.340000
 So that took a while.

0:09:22.340000 --> 0:09:23.760000
 So our initial query worked.

0:09:23.760000 --> 0:09:30.440000
 So 378 cities in the state of Florida
 have a population greater than 15

0:09:30.440000 --> 0:09:33.460000
,000 in the collection city
 in the database city.

0:09:33.460000 --> 0:09:36.200000
 We get rid of the account operator there.


0:09:36.200000 --> 0:09:38.220000
 It'll display them for us.

0:09:38.220000 --> 0:09:43.520000
 And yeah, so you sort of getting
 the hang of things here now.

0:09:43.520000 --> 0:09:48.820000
 I also want to show you the logical
 operator or and how to use that.

0:09:48.820000 --> 0:09:53.280000
 So we can ask ourselves the question,
 how many cities have a population

0:09:53.280000 --> 0:09:58.660000
 less than 100 or belong to the state
 of Florida or something like that.

0:09:58.660000 --> 0:10:01.800000
 So to do this, we would say DB dot city.

0:10:01.800000 --> 0:10:03.340000
 Let me type that in.

0:10:03.340000 --> 0:10:05.720000
 So DB dot city dot find.

0:10:05.720000 --> 0:10:12.380000
 And in here, we can say, for example,
 let's see, or so we'll say, or.

0:10:12.380000 --> 0:10:17.300000
 And then we put in the actual criteria
 whenever utilizing a logical operator,

0:10:17.300000 --> 0:10:20.060000
 you put in the logical operator,
 then the criteria.

0:10:20.060000 --> 0:10:22.660000
 So we're saying either one
 of them can be correct.

0:10:22.660000 --> 0:10:25.540000
 If either one of them is correct,
 display the results.

0:10:25.540000 --> 0:10:30.100000
 So in here, we'll say a population
 is going to be and then we'll open

0:10:30.100000 --> 0:10:37.740000
 up here. To say is less
 than we can say it.

0:10:37.740000 --> 0:10:41.040000
 So it is another logical operator here.

0:10:41.040000 --> 0:10:43.240000
 And then we set that to 100.

0:10:43.240000 --> 0:10:45.620000
 Actually not it lt.

0:10:45.620000 --> 0:10:46.820000
 Sorry, that's my bad.

0:10:46.820000 --> 0:10:52.360000
 So lt. And we say 100.

0:10:52.360000 --> 0:10:58.700000
 And then we can say in
 here, put in the state.

0:10:58.700000 --> 0:11:02.220000
 And that's going to be equal to Florida.

0:11:02.220000 --> 0:11:08.000000
 So FL. And yeah, so we'll close
 the first bracket here.

0:11:08.000000 --> 0:11:13.280000
 And then of course we need to close
 the the square bracket and then the

0:11:13.280000 --> 0:11:18.960000
 curly braces again for the initial set
 there and then the primary bracket

0:11:18.960000 --> 0:11:21.800000
 there. And we say count.

0:11:21.800000 --> 0:11:24.760000
 So one four nine nine.

0:11:24.760000 --> 0:11:32.800000
 So in essence, how many cities have
 a population less than a hundred?

0:11:32.800000 --> 0:11:38.560000
 Or belong to the state Florida in the
 collection city in the database

0:11:38.560000 --> 0:11:44.500000
 city. So we've talked about and or
 and less than greater than etc.

0:11:44.500000 --> 0:11:48.260000
 So you know, these would typically
 be so greater than in this case is

0:11:48.260000 --> 0:11:55.140000
 just dollar symbol GT less than
 is just dollar symbol lt.

0:11:55.140000 --> 0:11:59.140000
 The or operator is just or.

0:11:59.140000 --> 0:12:03.920000
 And then we have of course
 and and we also have not.

0:12:03.920000 --> 0:12:08.860000
 So that's you know, how we can learn
 around these logical operators.

0:12:08.860000 --> 0:12:11.860000
 We can also utilize rejects.

0:12:11.860000 --> 0:12:17.500000
 That's one of the great things with the
 no sequel or the Mongo query language

0:12:17.500000 --> 0:12:21.440000
 as it were. The way we can do this
 is let's say we want to find cities

0:12:21.440000 --> 0:12:27.600000
 that have their name starting
 with let's say.

0:12:27.600000 --> 0:12:35.720000
 Let's see. Let's see city starting with.

0:12:35.720000 --> 0:12:39.660000
 Let's try any OK, that
 might be interesting.

0:12:39.660000 --> 0:12:46.660000
 So we can say. Let's say use city.

0:12:46.660000 --> 0:12:50.220000
 We're currently using city here just
 trying to think of how we can run

0:12:50.220000 --> 0:12:53.160000
 the query. So we'll say
 DB dot city dot find.

0:12:53.160000 --> 0:12:58.200000
 And in here will say the will open up
 brackets and in here will say the

0:12:58.200000 --> 0:13:05.880000
 city field is. We're going to need to
 utilize rejects will open up curly

0:13:05.880000 --> 0:13:10.060000
 brackets here and will say
 dollar symbol rejects.

0:13:10.060000 --> 0:13:21.900000
 And then we say rejects is going to be.

0:13:21.900000 --> 0:13:27.300000
 So. So any actually know
 that might not work.

0:13:27.300000 --> 0:13:34.540000
 Let's just try just trying to think
 here we put in the rejects.

0:13:34.540000 --> 0:13:43.640000
 So. Starts with and then
 we can say for example.

0:13:43.640000 --> 0:13:55.800000
 Let's say. So if we take a look at
 this here the city is in capital so

0:13:55.800000 --> 0:14:05.040000
 we could say. So let's try H a for
 example just as an example and then

0:14:05.040000 --> 0:14:07.060000
 we'll use wildcard there.

0:14:07.060000 --> 0:14:12.700000
 And we'll close the curly brackets here
 and then the single the primary

0:14:12.700000 --> 0:14:14.420000
 bracket as it were.

0:14:14.420000 --> 0:14:16.900000
 And we'll just list them out.

0:14:16.900000 --> 0:14:20.440000
 So looks like we have an
 issue with our query.

0:14:20.440000 --> 0:14:24.100000
 So that's this one here this one here.

0:14:24.100000 --> 0:14:30.260000
 To be.city.find and we close that there
 rejects H a you may have an issue

0:14:30.260000 --> 0:14:32.860000
 with our rejects here.

0:14:32.860000 --> 0:14:42.100000
 Let's try H. Unterminated string
 literal that's interesting.

0:14:42.100000 --> 0:14:47.340000
 So I put out there we are okay my bad
 did not put the double quotes there.

0:14:47.340000 --> 0:14:52.020000
 There we are so anything starting
 with H is going to be listed here.

0:14:52.020000 --> 0:14:55.880000
 You can use rejects to do that again
 depending on what you want to do.

0:14:55.880000 --> 0:14:59.700000
 So for example we can say in this case.

0:14:59.700000 --> 0:15:04.940000
 We say H.O. that display Houston.

0:15:04.940000 --> 0:15:09.180000
 No we don't have that at least in this
 data set or collection if we try

0:15:09.180000 --> 0:15:13.580000
 for example. MI for Miami.

0:15:13.580000 --> 0:15:18.500000
 We should see a match there for Miami
 hopefully if it's been added.

0:15:18.500000 --> 0:15:21.400000
 So we only have Milan nothing else.

0:15:21.400000 --> 0:15:30.120000
 Let's try. Let's try something else.

0:15:30.120000 --> 0:15:36.180000
 Let's see. SA San Francisco.

0:15:36.180000 --> 0:15:39.960000
 So these are just cities Sanco Sanford
 Yah right so you get the idea you

0:15:39.960000 --> 0:15:42.280000
 can also utilize rejects.

0:15:42.280000 --> 0:15:48.480000
 And I've already shown you the logical
 operators and what we can do there.

0:15:48.480000 --> 0:15:52.120000
 We can also perform mathematical operations
 which is going to be the final

0:15:52.120000 --> 0:15:54.300000
 example here that I'll show you.

0:15:54.300000 --> 0:15:59.360000
 So while still working in the city database
 and the city collection what

0:15:59.360000 --> 0:16:04.280000
 if we wanted to find the average population
 to do that we would say db

0:16:04.280000 --> 0:16:13.180000
.city.aggregate. db.city.aggregate and
 then we'll put in here the group

0:16:13.180000 --> 0:16:14.660000
 will utilize the group functionality.

0:16:14.660000 --> 0:16:20.480000
 So this is used to group data documents
 rather within a collection and

0:16:20.480000 --> 0:16:22.520000
 then we'll say in here.

0:16:22.520000 --> 0:16:26.440000
 We'll say the ID.

0:16:26.440000 --> 0:16:29.660000
 So ID there's the field we want here.

0:16:29.660000 --> 0:16:31.740000
 Actually we need to put
 that in double quotes.

0:16:31.740000 --> 0:16:41.520000
 So we'll say ID and then we're going
 to say null average so a v g and

0:16:41.520000 --> 0:16:54.020000
 then in here. Let's see what I want specifically
 is to we'll need to utilize

0:16:54.020000 --> 0:16:55.480000
 the average operator.

0:16:55.480000 --> 0:17:00.360000
 So a v g and then in here we would put
 in the parameter or the field here.

0:17:00.360000 --> 0:17:05.120000
 So just population and then we
 would close that one there.

0:17:05.120000 --> 0:17:12.560000
 The primary one and the secondary and
 tertiary bracket and then in here

0:17:12.560000 --> 0:17:15.920000
 we can just hit enter.

0:17:15.920000 --> 0:17:18.720000
 And string literal again.

0:17:18.720000 --> 0:17:25.940000
 So group. Yeah I forgot to put this
 here in double quotes hit enter.

0:17:25.940000 --> 0:17:32.840000
 So the average in this particular
 case is 8462 which is interesting.

0:17:32.840000 --> 0:17:39.520000
 So there we are that's how to you know
 perform an average on a aggregate

0:17:39.520000 --> 0:17:43.560000
 of documents as it were within
 a particular collection.

0:17:43.560000 --> 0:17:48.100000
 With that being said that is the Mongo
 query language in a nutshell.

0:17:48.100000 --> 0:17:50.940000
 I've not covered everything but hopefully
 this will give you an understanding

0:17:50.940000 --> 0:17:55.380000
 as to how this differs from the
 structured query language.

0:17:55.380000 --> 0:17:58.300000
 And with that being said that's going to
 conclude the practical demonstration

0:17:58.300000 --> 0:18:00.500000
 side of this video.

0:18:00.500000 --> 0:18:06.360000
 All right so that was an introduction
 to no SQL databases and we got an

0:18:06.360000 --> 0:18:11.400000
 understanding as to the differences between
 SQL databases and no SQL databases.

0:18:11.400000 --> 0:18:16.440000
 Examples of no SQL databases how they
 work with regards to how they store

0:18:16.440000 --> 0:18:21.120000
 data. And we also took a look at a
 very popular no SQL database called

0:18:21.120000 --> 0:18:26.520000
 Mongo DB as well as how to use it or
 interact with it using the Mongo

0:18:26.520000 --> 0:18:29.300000
 query language or MQL.

0:18:29.300000 --> 0:18:32.740000
 In the next video we're going to be
 taking a look at a practical example

0:18:32.740000 --> 0:18:38.520000
 of what no SQL injection looks
 like primarily on Mongo DB.

0:18:38.520000 --> 0:18:41.920000
 With that being said I'll be
 seeing you in the next video.

