Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import Mongoose in an Express app.
Express
const mongoose = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'express' instead of 'mongoose'.
Using a package unrelated to schemas like 'cors'.
✗ Incorrect
You need to import mongoose to define schemas and models.
2fill in blank
mediumComplete the code to create a new Mongoose schema.
Express
const userSchema = new mongoose.[1]({ name: String, age: Number }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Model' instead of 'Schema' to define structure.
Confusing 'Document' or 'Collection' with schema.
✗ Incorrect
The Schema class defines the structure of documents in a collection.
3fill in blank
hardFix the error in the code to create a model from a schema.
Express
const User = mongoose.model('[1]', userSchema);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or plural names like 'users'.
Passing the schema variable name instead of model name.
✗ Incorrect
The first argument to mongoose.model is the singular model name, capitalized.
4fill in blank
hardFill both blanks to define a schema with a required string field and create a model.
Express
const productSchema = new mongoose.[1]({ title: { type: String, [2]: true } }); const Product = mongoose.model('Product', productSchema);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'unique' instead of 'required' for mandatory fields.
Confusing 'default' with 'required'.
✗ Incorrect
Use Schema to define the schema and required: true to make the field mandatory.
5fill in blank
hardFill all three blanks to define a schema with a default number field, create a model, and export it.
Express
const orderSchema = new mongoose.[1]({ quantity: { type: Number, [2]: 1 } }); const Order = mongoose.model('[3]', orderSchema); module.exports = Order;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Model' instead of 'Schema' for the first blank.
Using 'required' instead of 'default' for the second blank.
Using lowercase or plural model names.
✗ Incorrect
Define the schema with Schema, set a default value with default, and name the model 'Order'.