save() v/s insert()

MongoDB Fundamentals Dealing with data
1 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 take an example where I have a variable p one equals to an empty object. This time, we will also assign an underscore ID equals to one, p one dot name equals to Smith. P one dot c d equals to the last. If I say DB dot contracts dot insert p one and run the same, you will see that it gets executed successfully. And it has given a result saying that the N inserted is one. However, if I run the same thing, once more, we get an error because the underscore ID is more or less like a primary key of our DBMS tables.

So we cannot have the same ID. Now sometimes this may be a problem because we might have retrieved the data from the database and then we would have more terrified and we would like to save it back. So instead of the insert, if I use a save function and run the same, you will see that there is no error. However, this time it says number of match two documents is one, absurd it is zero, we'll come to the concept of observing but at this time, it is number of modified records is also zero. That's because we didn't make any changes. However, instead of Smith, this is Martin and save this and run the same.

You will see that this time it says number of modified documents is one. And that's because we we have some different values. It did not insert but it's just updating the existing document itself.

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.