0
0
NestJSframework~20 mins

Schema definition in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Schema Master
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 NestJS schema validation?
Given this schema and input, what will be the validation result?
NestJS
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

@Schema()
export class User extends Document {
  @Prop({ required: true })
  name: string;

  @Prop({ default: 18 })
  age: number;
}

const UserSchema = SchemaFactory.createForClass(User);

// Input data:
const input = { name: 'Alice' };
AValidation fails because age is missing
BValidation passes, age is set to 18 by default
CValidation passes, age is undefined
DValidation fails because name is missing
Attempts:
2 left
💡 Hint
Check the @Prop decorator options for default and required
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a nested schema in NestJS?
You want to define a nested Address schema inside a User schema. Which code snippet is correct?
A
class Address {
  @Prop() street: string;
  @Prop() city: string;
}

@Schema()
class User {
  @Prop({ type: () => Address }) address: Address;
}
B
@Schema()
class Address {
  @Prop() street: string;
  @Prop() city: string;
}

@Schema()
class User {
  @Prop({ type: Address }) address: Address;
}
C
@Schema()
class Address {
  @Prop() street: string;
  @Prop() city: string;
}

@Schema()
class User {
  @Prop({ type: () => Address }) address: Address;
}
D
class Address {
  @Prop() street: string;
  @Prop() city: string;
}

@Schema()
class User {
  @Prop({ type: Address }) address: Address;
}
Attempts:
2 left
💡 Hint
Nested schemas require both classes to be decorated with @Schema and use a function for type.
🔧 Debug
advanced
2:00remaining
Why does this NestJS schema throw a runtime error?
This schema code throws an error when creating a Product document. What is the cause?
NestJS
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

@Schema()
export class Product extends Document {
  @Prop()
  name: string;

  @Prop({ type: Number, required: true })
  price: number;
}

export const ProductSchema = SchemaFactory.createForClass(Product);

// Error: Missing required property 'price' when creating a Product document
AThe 'price' property is missing a default value and is required, causing error if not provided
BThe @Prop decorator must not include 'type' when using TypeScript types
CThe class should not extend Document when using SchemaFactory
DThe 'name' property is missing 'required: true' causing schema validation failure
Attempts:
2 left
💡 Hint
Check which properties are required and if defaults exist
state_output
advanced
2:00remaining
What is the value of 'createdAt' after saving this schema?
Given this schema with timestamps enabled, what will be the value of 'createdAt' after saving a new document?
NestJS
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

@Schema({ timestamps: true })
export class Article extends Document {
  @Prop() title: string;
}

const ArticleSchema = SchemaFactory.createForClass(Article);

// After saving a new Article with title 'Hello', what is article.createdAt?
ANull because timestamps are disabled by default
BUndefined because 'createdAt' is not defined in the class
CA string with the ISO date format
DA Date object representing the creation time
Attempts:
2 left
💡 Hint
Timestamps option automatically adds createdAt and updatedAt fields as Date objects
🧠 Conceptual
expert
2:00remaining
Which option best explains the purpose of SchemaFactory in NestJS?
Why does NestJS use SchemaFactory.createForClass instead of directly using Mongoose schemas?
AIt generates a Mongoose schema from a decorated TypeScript class, enabling decorators to define schema properties
BIt replaces the need for Mongoose entirely by providing a custom schema system
CIt compiles TypeScript classes into JavaScript functions for faster runtime performance
DIt automatically connects the schema to the database without manual model creation
Attempts:
2 left
💡 Hint
Think about how decorators and classes relate to Mongoose schemas