Challenge - 5 Problems
Schema Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Mongoose schema validation?
Consider this Mongoose schema for a User model. What happens if you try to save a user with an empty email field?
Express
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ email: { type: String, required: true }, name: String }); const User = mongoose.model('User', userSchema); const user = new User({ name: 'Alice' }); user.save().catch(err => console.log(err.message));
Attempts:
2 left
💡 Hint
Check the 'required' property in the schema definition.
✗ Incorrect
The 'required: true' in the schema means Mongoose will throw a validation error if the email field is missing when saving.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a Mongoose schema with a nested object?
You want to define a schema for a BlogPost with a nested author object containing name and age. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Nested objects can be defined directly as plain objects inside the schema.
✗ Incorrect
Option D correctly defines a nested object by using a plain object for 'author'. Options A and C misuse Schema constructor, and D has invalid syntax.
🔧 Debug
advanced2:00remaining
Why does this Mongoose model throw a 'Missing schema' error?
You have this code snippet. Why does it throw an error when trying to create a model?
Express
const mongoose = require('mongoose'); const userSchema = mongoose.Schema({ username: String }); const User = mongoose.model('User');
Attempts:
2 left
💡 Hint
Check the parameters passed to mongoose.model.
✗ Incorrect
The model function requires the schema as the second argument. Omitting it causes a 'Missing schema' error.
❓ state_output
advanced2:00remaining
What is the value of 'user.isNew' after saving a new document?
Given this code, what will be the value of 'user.isNew' after calling save() successfully?
Express
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ name: String }); const User = mongoose.model('User', userSchema); async function run() { const user = new User({ name: 'Bob' }); console.log(user.isNew); await user.save(); console.log(user.isNew); } run();
Attempts:
2 left
💡 Hint
Check the meaning of 'isNew' property in Mongoose documents.
✗ Incorrect
'isNew' is true for unsaved documents and becomes false after saving to the database.
🧠 Conceptual
expert2:00remaining
Which option best describes the purpose of Mongoose middleware in schemas?
What is the main use of middleware functions in Mongoose schemas?
Attempts:
2 left
💡 Hint
Think about hooks that run during document lifecycle events.
✗ Incorrect
Middleware in Mongoose allows running code before or after events like saving or removing documents.