Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Mongoose library.
Express
const mongoose = require([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'mongoose'.
Forgetting the quotes around the package name.
✗ Incorrect
We import Mongoose by requiring the "mongoose" package.
2fill in blank
mediumComplete the code to define 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'.
Confusing schema with collection.
✗ Incorrect
We create a schema using mongoose.Schema.
3fill in blank
hardFix the error in the code to create a model from the schema.
Express
const User = mongoose.model('User', [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling userSchema as a function.
Using capitalized 'UserSchema' when the variable is lowercase.
✗ Incorrect
The model function expects the schema object, which is userSchema.
4fill in blank
hardFill both blanks to define a schema with a required string field and a number field.
Express
const productSchema = new mongoose.[1]({ name: { type: String, [2]: true }, price: Number });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'unique' instead of 'required' for mandatory fields.
Using 'Model' instead of 'Schema' for schema definition.
✗ Incorrect
We use Schema to define the schema and required: true to make the field mandatory.
5fill in blank
hardFill all three blanks to create and export a Mongoose model named 'Order' with a schema having a date and total fields.
Express
const orderSchema = new mongoose.[1]({ date: Date, total: Number }); const Order = mongoose.[2]('Order', [3]); module.exports = Order;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Document' instead of 'Schema'.
Passing the model name instead of the schema to mongoose.model.
✗ Incorrect
We define the schema with Schema, create the model with model, and pass the schema variable orderSchema.