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
€46.21
List Price:  €64.70
You save:  €18.49
£38.66
List Price:  £54.13
You save:  £15.47
CA$71.74
List Price:  CA$100.45
You save:  CA$28.70
A$79.97
List Price:  A$111.96
You save:  A$31.99
S$67.10
List Price:  S$93.94
You save:  S$26.84
HK$388.86
List Price:  HK$544.43
You save:  HK$155.57
CHF 44.06
List Price:  CHF 61.68
You save:  CHF 17.62
NOK kr527.71
List Price:  NOK kr738.84
You save:  NOK kr211.12
DKK kr344.82
List Price:  DKK kr482.78
You save:  DKK kr137.95
NZ$87.93
List Price:  NZ$123.11
You save:  NZ$35.18
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.45
৳6,033.11
List Price:  ৳8,446.84
You save:  ৳2,413.72
₹4,270.28
List Price:  ₹5,978.73
You save:  ₹1,708.45
RM221.63
List Price:  RM310.30
You save:  RM88.67
₦76,577.18
List Price:  ₦107,214.18
You save:  ₦30,637
₨13,915.29
List Price:  ₨19,482.53
You save:  ₨5,567.23
฿1,695.56
List Price:  ฿2,373.92
You save:  ฿678.36
₺1,896.83
List Price:  ₺2,655.71
You save:  ₺758.88
B$289.90
List Price:  B$405.88
You save:  B$115.98
R914.89
List Price:  R1,280.93
You save:  R366.03
Лв90.33
List Price:  Лв126.47
You save:  Лв36.14
₩73,737.90
List Price:  ₩103,238.97
You save:  ₩29,501.06
₪185.76
List Price:  ₪260.07
You save:  ₪74.31
₱2,860.47
List Price:  ₱4,004.89
You save:  ₱1,144.41
¥7,458.48
List Price:  ¥10,442.47
You save:  ¥2,983.99
MX$1,019.93
List Price:  MX$1,427.99
You save:  MX$408.05
QR181
List Price:  QR253.41
You save:  QR72.41
P678.33
List Price:  P949.71
You save:  P271.38
KSh6,413.71
List Price:  KSh8,979.71
You save:  KSh2,566
E£2,528
List Price:  E£3,539.40
You save:  E£1,011.40
ብር6,523.41
List Price:  ብር9,133.30
You save:  ብር2,609.88
Kz45,590.88
List Price:  Kz63,830.88
You save:  Kz18,240
CLP$46,633.08
List Price:  CLP$65,290.05
You save:  CLP$18,656.96
CN¥362.58
List Price:  CN¥507.65
You save:  CN¥145.06
RD$3,133.95
List Price:  RD$4,387.78
You save:  RD$1,253.83
DA6,684.76
List Price:  DA9,359.20
You save:  DA2,674.44
FJ$116.33
List Price:  FJ$162.88
You save:  FJ$46.54
Q382.94
List Price:  Q536.15
You save:  Q153.20
GY$10,385.13
List Price:  GY$14,540.02
You save:  GY$4,154.88
ISK kr6,585.68
List Price:  ISK kr9,220.48
You save:  ISK kr2,634.80
DH480.22
List Price:  DH672.34
You save:  DH192.12
L895.47
List Price:  L1,253.73
You save:  L358.26
ден2,842.32
List Price:  ден3,979.48
You save:  ден1,137.15
MOP$397.74
List Price:  MOP$556.87
You save:  MOP$159.13
N$902.08
List Price:  N$1,262.98
You save:  N$360.90
C$1,827.24
List Price:  C$2,558.29
You save:  C$731.04
रु6,789.17
List Price:  रु9,505.39
You save:  रु2,716.21
S/180.70
List Price:  S/253
You save:  S/72.29
K204.65
List Price:  K286.53
You save:  K81.87
SAR187.51
List Price:  SAR262.53
You save:  SAR75.02
ZK1,413.86
List Price:  ZK1,979.52
You save:  ZK565.65
L230.02
List Price:  L322.05
You save:  L92.03
Kč1,154.98
List Price:  Kč1,617.07
You save:  Kč462.08
Ft18,579.03
List Price:  Ft26,012.13
You save:  Ft7,433.10
SEK kr501.68
List Price:  SEK kr702.39
You save:  SEK kr200.71
ARS$53,525.80
List Price:  ARS$74,940.41
You save:  ARS$21,414.60
Bs342.99
List Price:  Bs480.21
You save:  Bs137.22
COP$207,668.51
List Price:  COP$290,752.54
You save:  COP$83,084.02
₡24,812.59
List Price:  ₡34,739.62
You save:  ₡9,927.02
L1,270.19
List Price:  L1,778.38
You save:  L508.18
₲397,763.26
List Price:  ₲556,900.40
You save:  ₲159,137.13
$U2,091.22
List Price:  $U2,927.87
You save:  $U836.65
zł193.45
List Price:  zł270.85
You save:  zł77.39
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.