Updating multiple documents

MongoDB Fundamentals Updating and deleting
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
€44.43
List Price:  €62.21
You save:  €17.77
£37.59
List Price:  £52.63
You save:  £15.03
CA$69.69
List Price:  CA$97.58
You save:  CA$27.88
A$77.99
List Price:  A$109.20
You save:  A$31.20
S$64.90
List Price:  S$90.87
You save:  S$25.96
HK$388.85
List Price:  HK$544.43
You save:  HK$155.57
CHF 41.57
List Price:  CHF 58.21
You save:  CHF 16.63
NOK kr518.43
List Price:  NOK kr725.84
You save:  NOK kr207.41
DKK kr331.53
List Price:  DKK kr464.18
You save:  DKK kr132.64
NZ$84.57
List Price:  NZ$118.40
You save:  NZ$33.83
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.46
৳6,093.85
List Price:  ৳8,531.88
You save:  ৳2,438.02
₹4,269.78
List Price:  ₹5,978.04
You save:  ₹1,708.25
RM215.03
List Price:  RM301.06
You save:  RM86.03
₦80,339.42
List Price:  ₦112,481.62
You save:  ₦32,142.20
₨14,092.14
List Price:  ₨19,730.13
You save:  ₨5,637.98
฿1,648.17
List Price:  ฿2,307.57
You save:  ฿659.39
₺1,937.88
List Price:  ₺2,713.19
You save:  ₺775.30
B$282.36
List Price:  B$395.33
You save:  B$112.97
R909.80
List Price:  R1,273.80
You save:  R363.99
Лв86.89
List Price:  Лв121.66
You save:  Лв34.76
₩69,780.43
List Price:  ₩97,698.18
You save:  ₩27,917.75
₪177.65
List Price:  ₪248.73
You save:  ₪71.07
₱2,767.79
List Price:  ₱3,875.13
You save:  ₱1,107.34
¥7,270.27
List Price:  ¥10,178.95
You save:  ¥2,908.68
MX$971.97
List Price:  MX$1,360.84
You save:  MX$388.86
QR182.01
List Price:  QR254.83
You save:  QR72.82
P680.79
List Price:  P953.17
You save:  P272.37
KSh6,466.20
List Price:  KSh9,053.20
You save:  KSh2,587
E£2,529.10
List Price:  E£3,540.94
You save:  E£1,011.84
ብር6,728.99
List Price:  ብር9,421.13
You save:  ብር2,692.13
Kz45,590.88
List Price:  Kz63,830.88
You save:  Kz18,240
CLP$46,732.39
List Price:  CLP$65,429.09
You save:  CLP$18,696.69
CN¥361.19
List Price:  CN¥505.70
You save:  CN¥144.50
RD$2,951.01
List Price:  RD$4,131.65
You save:  RD$1,180.64
DA6,652.73
List Price:  DA9,314.35
You save:  DA2,661.62
FJ$113.56
List Price:  FJ$159
You save:  FJ$45.43
Q385.11
List Price:  Q539.19
You save:  Q154.07
GY$10,461.12
List Price:  GY$14,646.40
You save:  GY$4,185.28
ISK kr6,524.97
List Price:  ISK kr9,135.48
You save:  ISK kr2,610.51
DH462.53
List Price:  DH647.58
You save:  DH185.05
L854.60
List Price:  L1,196.51
You save:  L341.91
ден2,738.05
List Price:  ден3,833.49
You save:  ден1,095.44
MOP$400.55
List Price:  MOP$560.81
You save:  MOP$160.25
N$933.50
List Price:  N$1,306.98
You save:  N$373.47
C$1,845.09
List Price:  C$2,583.28
You save:  C$738.18
रु6,780.01
List Price:  रु9,492.56
You save:  रु2,712.54
S/182.01
List Price:  S/254.83
You save:  S/72.82
K202.87
List Price:  K284.03
You save:  K81.16
SAR187.51
List Price:  SAR262.53
You save:  SAR75.01
ZK1,390.76
List Price:  ZK1,947.17
You save:  ZK556.41
L227.47
List Price:  L318.48
You save:  L91
Kč1,108.70
List Price:  Kč1,552.28
You save:  Kč443.57
Ft17,958.90
List Price:  Ft25,143.90
You save:  Ft7,185
SEK kr485.48
List Price:  SEK kr679.72
You save:  SEK kr194.23
ARS$55,571.28
List Price:  ARS$77,804.24
You save:  ARS$22,232.95
Bs345.29
List Price:  Bs483.44
You save:  Bs138.14
COP$212,466.38
List Price:  COP$297,469.93
You save:  COP$85,003.55
₡25,326.52
List Price:  ₡35,459.16
You save:  ₡10,132.63
L1,301.19
List Price:  L1,821.77
You save:  L520.58
₲401,593.41
List Price:  ₲562,262.91
You save:  ₲160,669.49
$U2,088.44
List Price:  $U2,923.98
You save:  $U835.54
zł188.05
List Price:  zł263.29
You save:  zł75.23
Already have an account? Log In

Transcript

Let's say we want to increment the price of all the products of fresher brand. So first, let's find out what are those products. So we get into the Mongo shell, test database. And then I'll say DB dot, products dot find. And I'll get the condition as brand is fresher. And we can also say pretty, so that we can see the results properly.

So there are many of them. Let's see the last few of them were the product price happens to be 2935 and 39. And we want to check if that increases the product price for all of this. So let's write the query for the same thing. So we said dv dot products dot update. And the criteria happens to be brand equals to fresher.

And then to increment the price we can say dollar Learn IMC for increment, we say unit price and the amount being incremented. Let's say we want to increment this by five rupees. So, it says here that find all the products with this brand fresh Oh, and then increment the unit price by five. So if it is 30, it should become 3535 becomes 40 and so on. So, let's see if this works. Save this and execute this.

When I execute, it says number of documents matched one. Absurd is zero modified is one but we saw there are many, many documents matching that condition. But there was only one update to place. Now this is because by default MongoDB updates the very first document that it finds now since in this case it was quite arbitrary. We don't get the desired result. You can Go and verify that also.

Alternately, we go here and then specify a third option where we earlier give absurd this time we don't want to really absurd but we can say here, multi AZ true. Now by default multi has been set as false. So we save this, we run the same thing again and you can see it this time it says matched documents or 56. Modified documents are 56. So we can also go to the Mongo shell, and check DB dot products dot find. And we say brand threshold.

We'll also say pretty. So if you remember the last few outputs, we had 35. For this, we now have 40. We had 39, and it is 44. And we had 29. That's changed to 34.

So this way, we can update multiple documents as well.

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.