0
0
Expressframework~20 mins

Defining schemas and models in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Schema Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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));
AIt throws a validation error because email is required
BIt saves but email is saved as an empty string
CIt throws a syntax error due to missing email
DIt saves successfully with email as undefined
Attempts:
2 left
💡 Hint
Check the 'required' property in the schema definition.
📝 Syntax
intermediate
2: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?
Aconst blogSchema = new mongoose.Schema({ title: String, author: { type: Object, name: String, age: Number } });
Bconst blogSchema = new mongoose.Schema({ title: String, author: mongoose.Schema({ name: String, age: Number }) });
Cconst blogSchema = new mongoose.Schema({ title: String, author: new mongoose.Schema({ name: String, age: Number }) });
Dconst blogSchema = new mongoose.Schema({ title: String, author: { name: String, age: Number } });
Attempts:
2 left
💡 Hint
Nested objects can be defined directly as plain objects inside the schema.
🔧 Debug
advanced
2: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');
ABecause the schema is missing a required field
BBecause the model is created without passing the schema as the second argument
CBecause mongoose.Schema is not a function
DBecause the model name 'User' is reserved
Attempts:
2 left
💡 Hint
Check the parameters passed to mongoose.model.
state_output
advanced
2: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();
Atrue before save, false after save
Bfalse before save, true after save
Ctrue both before and after save
Dfalse both before and after save
Attempts:
2 left
💡 Hint
Check the meaning of 'isNew' property in Mongoose documents.
🧠 Conceptual
expert
2:00remaining
Which option best describes the purpose of Mongoose middleware in schemas?
What is the main use of middleware functions in Mongoose schemas?
ATo define custom validation rules for schema fields
BTo automatically create indexes on schema fields
CTo run functions before or after certain schema methods like save or remove
DTo convert schema definitions into JSON objects
Attempts:
2 left
💡 Hint
Think about hooks that run during document lifecycle events.