Day 5 of 5
⏱ ~60 minutes
MongoDB in 5 Days — Day 5

Atlas and Mongoose

MongoDB Atlas for production, Mongoose for Node.js with schemas, validation, and populate.

MongoDB Atlas Setup

Atlas setup
# 1. Sign up at mongodb.com/atlas (free tier = 512MB)
# 2. Create a cluster (M0 = free)
# 3. Create a database user
# 4. Add your IP to the allowlist
# 5. Get connection string:
# mongodb+srv://user:[email protected]/mydb
Mongoose Schema and Model
npm install mongoose

import mongoose from 'mongoose';
await mongoose.connect(process.env.MONGODB_URI);

const postSchema = new mongoose.Schema({
  title: { type: String, required: true, trim: true, maxlength: 200 },
  body: { type: String, required: true },
  author: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
  tags: [String],
  published: { type: Boolean, default: false },
  createdAt: { type: Date, default: Date.now },
});

postSchema.index({ title: 'text' });

export const Post = mongoose.model('Post', postSchema);
Mongoose CRUD and populate
// Create
const post = await Post.create({
  title: 'Hello',
  body: 'World',
  author: userId
});

// Read with populate (join)
const posts = await Post.find({ published: true })
  .populate('author', 'name email')  // only name and email
  .sort('-createdAt')
  .limit(20);

// Update
await Post.findByIdAndUpdate(id, { $set: { published: true } }, { new: true });

// Delete
await Post.findByIdAndDelete(id);
📝 Day 5 Exercise
Move to MongoDB Atlas
  1. C
  2. r
  3. e
  4. a
  5. t
  6. e
  7. a
  8. f
  9. r
  10. e
  11. e
  12. A
  13. t
  14. l
  15. a
  16. s
  17. c
  18. l
  19. u
  20. s
  21. t
  22. e
  23. r
  24. .
  25. C
  26. o
  27. n
  28. n
  29. e
  30. c
  31. t
  32. y
  33. o
  34. u
  35. r
  36. N
  37. o
  38. d
  39. e
  40. .
  41. j
  42. s
  43. a
  44. p
  45. p
  46. t
  47. o
  48. i
  49. t
  50. .
  51. V
  52. e
  53. r
  54. i
  55. f
  56. y
  57. d
  58. a
  59. t
  60. a
  61. p
  62. e
  63. r
  64. s
  65. i
  66. s
  67. t
  68. s
  69. a
  70. f
  71. t
  72. e
  73. r
  74. r
  75. e
  76. s
  77. t
  78. a
  79. r
  80. t
  81. i
  82. n
  83. g
  84. y
  85. o
  86. u
  87. r
  88. l
  89. o
  90. c
  91. a
  92. l
  93. s
  94. e
  95. r
  96. v
  97. e
  98. r
  99. .
  100. A
  101. d
  102. d
  103. a
  104. M
  105. o
  106. n
  107. g
  108. o
  109. o
  110. s
  111. e
  112. s
  113. c
  114. h
  115. e
  116. m
  117. a
  118. w
  119. i
  120. t
  121. h
  122. v
  123. a
  124. l
  125. i
  126. d
  127. a
  128. t
  129. i
  130. o
  131. n
  132. t
  133. e
  134. s
  135. t
  136. t
  137. h
  138. a
  139. t
  140. i
  141. t
  142. r
  143. e
  144. j
  145. e
  146. c
  147. t
  148. s
  149. i
  150. n
  151. v
  152. a
  153. l
  154. i
  155. d
  156. d
  157. a
  158. t
  159. a
  160. .

Day 5 Summary

  • Atlas free tier (M0) = 512MB storage, no credit card required. Good for development.
  • Mongoose schemas add structure and validation to MongoDB's flexible documents.
  • populate() performs a join: replaces ObjectId references with the actual documents.
  • ref: 'User' in the schema tells Mongoose which model to use for populate.
Finished this lesson?