0
0
Expressframework~20 mins

Mongoose ODM setup in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mongoose Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you connect to MongoDB with Mongoose using this code?

Consider this Express app snippet using Mongoose to connect to MongoDB:

import express from 'express';
import mongoose from 'mongoose';

const app = express();

mongoose.connect('mongodb://localhost:27017/mydb')
  .then(() => console.log('Connected to MongoDB'))
  .catch(err => console.error('Connection error:', err));

app.listen(3000, () => console.log('Server running'));

What will be printed if the MongoDB server is running and accessible?

Express
import express from 'express';
import mongoose from 'mongoose';

const app = express();

mongoose.connect('mongodb://localhost:27017/mydb')
  .then(() => console.log('Connected to MongoDB'))
  .catch(err => console.error('Connection error:', err));

app.listen(3000, () => console.log('Server running'));
AServer running only, no connection messages
BConnected to MongoDB\nServer running
CConnection error: Error: failed to connect\nServer running
DServer running\nConnected to MongoDB
Attempts:
2 left
💡 Hint

Promises run asynchronously. The server starts listening immediately, but connection success logs after connection is established.

📝 Syntax
intermediate
2:00remaining
Which Mongoose schema definition is valid for a User model with required name and optional age?

Choose the correct Mongoose schema code snippet that defines a User model with a required name (string) and an optional age (number).

Aconst userSchema = new mongoose.Schema({ name: { type: String, required: true }, age: Number });
Bconst userSchema = new mongoose.Schema({ name: String, required: true, age: Number });
Cconst userSchema = new mongoose.Schema({ name: String, age: { type: Number, required: false } });
Dconst userSchema = new mongoose.Schema({ name: { type: String }, age: { type: Number, required: true } });
Attempts:
2 left
💡 Hint

Remember that required is a property inside the field's object, not outside.

🔧 Debug
advanced
2:00remaining
Why does this Mongoose connection code cause an unhandled promise rejection?

Look at this code snippet:

mongoose.connect('mongodb://localhost:27017/mydb');

app.listen(3000, () => console.log('Server running'));

Why might this cause an unhandled promise rejection if the database is down?

Express
mongoose.connect('mongodb://localhost:27017/mydb');

app.listen(3000, () => console.log('Server running'));
ABecause app.listen must be called inside the .then() callback of mongoose.connect.
BBecause mongoose.connect requires a callback function as second argument to handle errors.
CBecause the promise returned by mongoose.connect is not handled with .then() or .catch(), so errors are unhandled.
DBecause the connection string is missing the protocol 'mongodb+srv://'.
Attempts:
2 left
💡 Hint

Promises must be handled to catch errors.

state_output
advanced
2:00remaining
What is the value of user.isNew after saving a new Mongoose document?

Given this code:

const userSchema = new mongoose.Schema({ name: String });
const User = mongoose.model('User', userSchema);

const user = new User({ name: 'Alice' });
console.log(user.isNew);
await user.save();
console.log(user.isNew);

What will be printed by the two console.log statements?

Express
const userSchema = new mongoose.Schema({ name: String });
const User = mongoose.model('User', userSchema);

const user = new User({ name: 'Alice' });
console.log(user.isNew);
await user.save();
console.log(user.isNew);
Afalse\ntrue
Btrue\nfalse
Ctrue\ntrue
Dfalse\nfalse
Attempts:
2 left
💡 Hint

isNew is true before saving and false after saving.

🧠 Conceptual
expert
2:00remaining
Which option best describes Mongoose middleware (hooks) behavior?

Mongoose supports middleware functions called hooks that run before or after certain operations.

Which statement correctly describes how pre and post hooks work in Mongoose?

APre hooks run before the operation and can modify the document; post hooks run after and receive the result or error.
BPre hooks run after the operation completes; post hooks run before the operation starts.
CPre hooks only run on queries; post hooks only run on document saves.
DMiddleware hooks in Mongoose are synchronous only and cannot handle async operations.
Attempts:
2 left
💡 Hint

Think about when you want to change data or react to results.