Complete the code to import the Express module.
const express = require('[1]');
The Express module is imported using require('express'). This allows us to create an Express app.
Complete the code to connect to a MongoDB database using Mongoose.
const mongoose = require('mongoose'); mongoose.connect('[1]');
To connect to MongoDB with Mongoose, use the MongoDB connection string starting with mongodb://.
Fix the error in the Express route that saves data to the database.
app.post('/add', async (req, res) => { const item = new Item({ name: req.body.[1] }); await item.save(); res.send('Saved'); });
The model expects a field called name. Using req.body.name correctly accesses the data sent by the client.
Fill both blanks to create a Mongoose schema and model for a user.
const userSchema = new mongoose.Schema({
[1]: String,
[2]: Number
});
const User = mongoose.model('User', userSchema);The schema defines a username as a string and age as a number, which are common user fields.
Fill all three blanks to query users older than 18 and sort by username.
User.find({ [1]: { [2]: 18 } })
.sort({ [3]: 1 })
.exec((err, users) => {
if (err) return console.error(err);
console.log(users);
});This query finds users where age is greater than ($gt) 18, then sorts them by username in ascending order.