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?
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'));
Promises run asynchronously. The server starts listening immediately, but connection success logs after connection is established.
The app.listen callback runs immediately after starting the server, printing 'Server running'. The mongoose.connect promise resolves after connecting, printing 'Connected to MongoDB'. So the output order is 'Server running' then 'Connected to MongoDB'.
Choose the correct Mongoose schema code snippet that defines a User model with a required name (string) and an optional age (number).
Remember that required is a property inside the field's object, not outside.
Option A correctly sets name as an object with type and required. Option A incorrectly places required outside the field definition. Option A makes age required false explicitly but misses name required. Option A makes age required true, which is wrong.
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?
mongoose.connect('mongodb://localhost:27017/mydb'); app.listen(3000, () => console.log('Server running'));
Promises must be handled to catch errors.
Calling mongoose.connect returns a promise. If the promise rejects (e.g., DB down) and no .catch() or try/catch handles it, Node.js raises an unhandled promise rejection error. Option C is not required; app.listen can run independently. Option C is outdated; mongoose.connect returns a promise now. Option C is incorrect because 'mongodb://' is valid for local connections.
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?
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);isNew is true before saving and false after saving.
When a Mongoose document is created but not saved, isNew is true. After calling save(), the document is stored in the database and isNew becomes false.
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?
Think about when you want to change data or react to results.
Pre hooks run before the operation and can modify data or cancel the operation. Post hooks run after the operation and receive the result or error. Hooks can be async. Option A reverses the order. Option A is incorrect because hooks can run on queries and documents. Option A is wrong because hooks support async functions.