Indexes

2 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€42.54
List Price:  €59.57
You save:  €17.02
£36.92
List Price:  £51.69
You save:  £14.77
CA$68.07
List Price:  CA$95.30
You save:  CA$27.23
A$75.65
List Price:  A$105.91
You save:  A$30.26
S$63.85
List Price:  S$89.40
You save:  S$25.54
HK$392.41
List Price:  HK$549.41
You save:  HK$156.99
CHF 39.72
List Price:  CHF 55.62
You save:  CHF 15.89
NOK kr505.09
List Price:  NOK kr707.18
You save:  NOK kr202.08
DKK kr317.58
List Price:  DKK kr444.63
You save:  DKK kr127.05
NZ$82.72
List Price:  NZ$115.81
You save:  NZ$33.09
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.46
৳6,072.47
List Price:  ৳8,501.94
You save:  ৳2,429.47
₹4,320.41
List Price:  ₹6,048.92
You save:  ₹1,728.51
RM210.73
List Price:  RM295.04
You save:  RM84.31
₦76,234.25
List Price:  ₦106,734.05
You save:  ₦30,499.80
₨14,182.38
List Price:  ₨19,856.47
You save:  ₨5,674.08
฿1,613.17
List Price:  ฿2,258.57
You save:  ฿645.40
₺2,023.41
List Price:  ₺2,832.94
You save:  ₺809.52
B$275.87
List Price:  B$386.24
You save:  B$110.37
R881
List Price:  R1,233.48
You save:  R352.47
Лв83.12
List Price:  Лв116.38
You save:  Лв33.25
₩68,546.70
List Price:  ₩95,970.87
You save:  ₩27,424.16
₪167.06
List Price:  ₪233.90
You save:  ₪66.84
₱2,832.13
List Price:  ₱3,965.21
You save:  ₱1,133.08
¥7,327.15
List Price:  ¥10,258.60
You save:  ¥2,931.45
MX$927.80
List Price:  MX$1,299
You save:  MX$371.19
QR181.60
List Price:  QR254.26
You save:  QR72.65
P664.85
List Price:  P930.84
You save:  P265.99
KSh6,458.70
List Price:  KSh9,042.70
You save:  KSh2,584
E£2,453.64
List Price:  E£3,435.29
You save:  E£981.65
ብር6,806.64
List Price:  ብር9,529.84
You save:  ብር2,723.20
Kz45,588.63
List Price:  Kz63,827.73
You save:  Kz18,239.10
CLP$47,418.51
List Price:  CLP$66,389.71
You save:  CLP$18,971.20
CN¥357.79
List Price:  CN¥500.94
You save:  CN¥143.14
RD$3,015.42
List Price:  RD$4,221.82
You save:  RD$1,206.40
DA6,471
List Price:  DA9,059.92
You save:  DA2,588.92
FJ$111.78
List Price:  FJ$156.51
You save:  FJ$44.72
Q382.34
List Price:  Q535.31
You save:  Q152.96
GY$10,416.66
List Price:  GY$14,584.16
You save:  GY$4,167.49
ISK kr6,041.79
List Price:  ISK kr8,458.99
You save:  ISK kr2,417.20
DH448.01
List Price:  DH627.25
You save:  DH179.24
L840.59
List Price:  L1,176.90
You save:  L336.30
ден2,615.24
List Price:  ден3,661.55
You save:  ден1,046.30
MOP$402.75
List Price:  MOP$563.89
You save:  MOP$161.13
N$874.78
List Price:  N$1,224.76
You save:  N$349.98
C$1,833.12
List Price:  C$2,566.52
You save:  C$733.39
रु6,886.73
List Price:  रु9,641.98
You save:  रु2,755.24
S/177.56
List Price:  S/248.60
You save:  S/71.04
K209.39
List Price:  K293.17
You save:  K83.77
SAR187.53
List Price:  SAR262.56
You save:  SAR75.02
ZK1,157.03
List Price:  ZK1,619.94
You save:  ZK462.90
L215.74
List Price:  L302.05
You save:  L86.31
Kč1,045.08
List Price:  Kč1,463.20
You save:  Kč418.11
Ft16,926.79
List Price:  Ft23,698.87
You save:  Ft6,772.07
SEK kr476.40
List Price:  SEK kr667.01
You save:  SEK kr190.60
ARS$62,911.61
List Price:  ARS$88,081.29
You save:  ARS$25,169.68
Bs344.23
List Price:  Bs481.96
You save:  Bs137.72
COP$203,254.31
List Price:  COP$284,572.29
You save:  COP$81,317.98
₡25,166.74
List Price:  ₡35,235.46
You save:  ₡10,068.71
L1,304.43
List Price:  L1,826.30
You save:  L521.87
₲373,143.55
List Price:  ₲522,430.83
You save:  ₲149,287.27
$U1,999.51
List Price:  $U2,799.48
You save:  $U799.96
zł181.08
List Price:  zł253.53
You save:  zł72.44
Already have an account? Log In

Transcript

By default, MongoDB scans every document in a collection to select those which match the query condition. If you have a query that uses a particular field and the query is used by the clients quite often, then we can create an index for the field so that the performance of the query can be increased. MongoDB offers different kinds of indexes, and the most common one is the single field index. Before we create this index, let's have a look at this command. We are searching a contact using a phone number, we can ask MongoDB as How did it arrive at this particular result by calling the explain function on the Find. The result shows which one is a winning plan and the winning plan says that it is called scan.

Basically it says the phone number was searched by matching this phone number for every document until it found or it is not found. This is quite inefficient if the query is repeatedly being used to avoid this We can now use a index. First, let's see what are the indexes that are currently available by giving DB dot contracts dot get indexes. And it says that there is only one single index on the field underscore ID underscore ID property is indexed by default for all collections. For example, db dot employees dot get indexes, also shows me that there is one index on the underscore ID. And we can go and check for all the collections and the story is saved.

Now let's create a new index for the phone property of contacts to do so DB dot contacts dot create index. And then we have to say phone to be indexed. Now it gives me a message saying that number of indexes before was one and number of indexes after is two. We can verify the same thing by issuing the command again, to get the indexes and do see that there are two of them. Now, we can also ask MongoDB to explain the query if we execute it now. And now you can see that out of many plans it came across with it now uses an eye x scan, which is nothing but index can if you want to drop the index DB dot context dot drop index, and then specify which is the property on which you want to drop the index.

So I have here phone as the property and then I can just say, drop indexes. Now it says number of indexes was two, which means now it should be only one we can also check it out by getting the indexes listed, which is that there is only one and also we can check the explained functions result which is now going back to the call scan.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.