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
€43.11
List Price:  €60.35
You save:  €17.24
£37.27
List Price:  £52.18
You save:  £14.91
CA$69.56
List Price:  CA$97.40
You save:  CA$27.83
A$77.88
List Price:  A$109.05
You save:  A$31.16
S$64.43
List Price:  S$90.22
You save:  S$25.78
HK$390.87
List Price:  HK$547.24
You save:  HK$156.37
CHF 40.43
List Price:  CHF 56.61
You save:  CHF 16.17
NOK kr510.81
List Price:  NOK kr715.17
You save:  NOK kr204.36
DKK kr321.79
List Price:  DKK kr450.53
You save:  DKK kr128.74
NZ$86.11
List Price:  NZ$120.57
You save:  NZ$34.45
د.إ183.58
List Price:  د.إ257.03
You save:  د.إ73.45
৳6,079.82
List Price:  ৳8,512.24
You save:  ৳2,432.41
₹4,374.64
List Price:  ₹6,124.85
You save:  ₹1,750.20
RM211.33
List Price:  RM295.88
You save:  RM84.55
₦76,885.61
List Price:  ₦107,646.01
You save:  ₦30,760.40
₨14,144.95
List Price:  ₨19,804.07
You save:  ₨5,659.11
฿1,630.67
List Price:  ฿2,283.07
You save:  ฿652.40
₺2,050.29
List Price:  ₺2,870.58
You save:  ₺820.28
B$273.18
List Price:  B$382.48
You save:  B$109.29
R881.64
List Price:  R1,234.37
You save:  R352.72
Лв84.30
List Price:  Лв118.02
You save:  Лв33.72
₩69,535.58
List Price:  ₩97,355.38
You save:  ₩27,819.79
₪170.06
List Price:  ₪238.10
You save:  ₪68.03
₱2,849.07
List Price:  ₱3,988.93
You save:  ₱1,139.85
¥7,430.09
List Price:  ¥10,402.73
You save:  ¥2,972.63
MX$936.01
List Price:  MX$1,310.48
You save:  MX$374.47
QR182.74
List Price:  QR255.85
You save:  QR73.11
P674.58
List Price:  P944.47
You save:  P269.88
KSh6,456.20
List Price:  KSh9,039.20
You save:  KSh2,583
E£2,423.14
List Price:  E£3,392.59
You save:  E£969.45
ብር7,087.94
List Price:  ብር9,923.69
You save:  ብር2,835.74
Kz45,588.63
List Price:  Kz63,827.73
You save:  Kz18,239.10
CLP$48,536.79
List Price:  CLP$67,955.39
You save:  CLP$19,418.60
CN¥358.88
List Price:  CN¥502.46
You save:  CN¥143.58
RD$3,111.53
List Price:  RD$4,356.40
You save:  RD$1,244.86
DA6,499.29
List Price:  DA9,099.52
You save:  DA2,600.23
FJ$113.79
List Price:  FJ$159.32
You save:  FJ$45.52
Q383.17
List Price:  Q536.47
You save:  Q153.29
GY$10,455.02
List Price:  GY$14,637.87
You save:  GY$4,182.84
ISK kr6,181.76
List Price:  ISK kr8,654.96
You save:  ISK kr2,473.20
DH452.30
List Price:  DH633.26
You save:  DH180.95
L842.08
List Price:  L1,178.98
You save:  L336.90
ден2,648.46
List Price:  ден3,708.05
You save:  ден1,059.59
MOP$402.17
List Price:  MOP$563.07
You save:  MOP$160.90
N$887.54
List Price:  N$1,242.63
You save:  N$355.08
C$1,845.04
List Price:  C$2,583.21
You save:  C$738.16
रु6,995.80
List Price:  रु9,794.68
You save:  रु2,798.88
S/174.96
List Price:  S/244.96
You save:  S/70
K211.91
List Price:  K296.70
You save:  K84.78
SAR187.58
List Price:  SAR262.63
You save:  SAR75.04
ZK1,159.42
List Price:  ZK1,623.29
You save:  ZK463.86
L217.88
List Price:  L305.05
You save:  L87.17
Kč1,059.72
List Price:  Kč1,483.70
You save:  Kč423.97
Ft17,075.30
List Price:  Ft23,906.78
You save:  Ft6,831.48
SEK kr481.96
List Price:  SEK kr674.78
You save:  SEK kr192.82
ARS$65,711.57
List Price:  ARS$92,001.45
You save:  ARS$26,289.88
Bs346.47
List Price:  Bs485.08
You save:  Bs138.61
COP$201,892.72
List Price:  COP$282,665.96
You save:  COP$80,773.24
₡25,215.26
List Price:  ₡35,303.39
You save:  ₡10,088.12
L1,311.53
List Price:  L1,836.25
You save:  L524.71
₲362,303.53
List Price:  ₲507,253.94
You save:  ₲144,950.40
$U2,000.70
List Price:  $U2,801.15
You save:  $U800.44
zł183.73
List Price:  zł257.23
You save:  zł73.50
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.