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.13
List Price:  €61.79
You save:  €17.65
£37.11
List Price:  £51.95
You save:  £14.84
CA$69.08
List Price:  CA$96.71
You save:  CA$27.63
A$77.89
List Price:  A$109.06
You save:  A$31.16
S$64.54
List Price:  S$90.36
You save:  S$25.82
HK$392.13
List Price:  HK$549.01
You save:  HK$156.88
CHF 41.19
List Price:  CHF 57.67
You save:  CHF 16.48
NOK kr509.24
List Price:  NOK kr712.98
You save:  NOK kr203.73
DKK kr329.24
List Price:  DKK kr460.96
You save:  DKK kr131.72
NZ$83.95
List Price:  NZ$117.53
You save:  NZ$33.58
د.إ183.60
List Price:  د.إ257.06
You save:  د.إ73.45
৳6,111.22
List Price:  ৳8,556.20
You save:  ৳2,444.97
₹4,279.27
List Price:  ₹5,991.32
You save:  ₹1,712.05
RM212.78
List Price:  RM297.91
You save:  RM85.13
₦79,491.59
List Price:  ₦111,294.59
You save:  ₦31,803
₨14,102.93
List Price:  ₨19,745.23
You save:  ₨5,642.30
฿1,641.12
List Price:  ฿2,297.70
You save:  ฿656.58
₺1,961.73
List Price:  ₺2,746.58
You save:  ₺784.85
B$283.46
List Price:  B$396.87
You save:  B$113.40
R893.53
List Price:  R1,251.02
You save:  R357.48
Лв86.23
List Price:  Лв120.74
You save:  Лв34.50
₩68,936.85
List Price:  ₩96,517.11
You save:  ₩27,580.25
₪175.84
List Price:  ₪246.19
You save:  ₪70.35
₱2,788.65
List Price:  ₱3,904.33
You save:  ₱1,115.68
¥7,200.74
List Price:  ¥10,081.62
You save:  ¥2,880.87
MX$964.74
List Price:  MX$1,350.72
You save:  MX$385.97
QR182.29
List Price:  QR255.22
You save:  QR72.93
P674.44
List Price:  P944.27
You save:  P269.83
KSh6,460.20
List Price:  KSh9,044.80
You save:  KSh2,584.60
E£2,485.55
List Price:  E£3,479.97
You save:  E£994.42
ብር6,793.31
List Price:  ብር9,511.17
You save:  ብር2,717.86
Kz45,588.63
List Price:  Kz63,827.73
You save:  Kz18,239.10
CLP$46,937.11
List Price:  CLP$65,715.71
You save:  CLP$18,778.60
CN¥359.70
List Price:  CN¥503.61
You save:  CN¥143.91
RD$2,952.81
List Price:  RD$4,134.17
You save:  RD$1,181.36
DA6,611.90
List Price:  DA9,257.20
You save:  DA2,645.29
FJ$113.17
List Price:  FJ$158.46
You save:  FJ$45.28
Q384.07
List Price:  Q537.74
You save:  Q153.66
GY$10,463.22
List Price:  GY$14,649.35
You save:  GY$4,186.12
ISK kr6,364.72
List Price:  ISK kr8,911.12
You save:  ISK kr2,546.40
DH462.80
List Price:  DH647.96
You save:  DH185.16
L867.35
List Price:  L1,214.37
You save:  L347.01
ден2,715.50
List Price:  ден3,801.92
You save:  ден1,086.41
MOP$403.84
List Price:  MOP$565.42
You save:  MOP$161.57
N$892.84
List Price:  N$1,250.06
You save:  N$357.21
C$1,840.19
List Price:  C$2,576.41
You save:  C$736.22
रु6,842.16
List Price:  रु9,579.57
You save:  रु2,737.41
S/182.08
List Price:  S/254.93
You save:  S/72.84
K208.30
List Price:  K291.64
You save:  K83.33
SAR187.52
List Price:  SAR262.55
You save:  SAR75.02
ZK1,326.77
List Price:  ZK1,857.58
You save:  ZK530.81
L223.47
List Price:  L312.88
You save:  L89.40
Kč1,099.14
List Price:  Kč1,538.88
You save:  Kč439.74
Ft17,821.84
List Price:  Ft24,952
You save:  Ft7,130.16
SEK kr479.88
List Price:  SEK kr671.88
You save:  SEK kr191.99
ARS$59,187.38
List Price:  ARS$82,867.06
You save:  ARS$23,679.68
Bs345.57
List Price:  Bs483.82
You save:  Bs138.25
COP$205,418.45
List Price:  COP$287,602.27
You save:  COP$82,183.82
₡25,460.48
List Price:  ₡35,646.71
You save:  ₡10,186.23
L1,303.02
List Price:  L1,824.33
You save:  L521.31
₲399,601.41
List Price:  ₲559,473.94
You save:  ₲159,872.53
$U2,081.90
List Price:  $U2,914.83
You save:  $U832.93
zł187.69
List Price:  zł262.78
You save:  zł75.09
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.