Node.js Database Integration 5 — Questions and Answers
Question 1: What is the purpose of `mongoose.connection.on('error', handler)` in a Node.js application?
- To handle errors thrown during schema validation
- To catch post-connection errors such as network drops (Correct answer)
- To intercept query errors before they reach the caller
- To log all MongoDB operations
Correct answer: To catch post-connection errors such as network drops
This event listener catches connection-level errors that occur after the initial connection is established, like network interruptions.
Question 2: In Sequelize, what is the difference between `Model.create()` and `Model.build()` + `instance.save()`?
- There is no difference — they produce the same result
- `create()` combines build and save in one call; `build()` creates an unsaved instance (Correct answer)
- `build()` skips validation; `create()` validates
- `create()` is for bulk inserts only
Correct answer: `create()` combines build and save in one call; `build()` creates an unsaved instance
`Model.create()` is a convenience method that builds and persists in one step, while `build()` creates an in-memory instance you must manually `save()`.
Question 3: Which Redis command atomically increments an integer value stored at a key?
- client.add('counter', 1)
- client.incr('counter') (Correct answer)
- client.update('counter', '+1')
- client.set('counter', counter + 1)
Correct answer: client.incr('counter')
`INCR` atomically increments the integer value at a key by 1, making it safe for concurrent access without locks.
Question 4: In Prisma, what is a `select` clause used for in a `findMany` call?
- Filtering which records to return
- Choosing which fields to include in the result (Correct answer)
- Sorting the result set
- Joining related models
Correct answer: Choosing which fields to include in the result
The `select` option lets you specify exactly which fields Prisma should return, reducing payload size.
Question 5: What does enabling `WAL mode` (Write-Ahead Logging) in SQLite improve for a Node.js application?
- Encryption of data at rest
- Concurrent read performance while a write is in progress (Correct answer)
- Automatic backups to cloud storage
- Connection pooling across processes
Correct answer: Concurrent read performance while a write is in progress
WAL mode allows reads and writes to occur concurrently without blocking each other, which is critical for web servers.
Question 6: In MongoDB, what is the `$lookup` aggregation stage equivalent to in SQL?
- WHERE clause
- GROUP BY clause
- JOIN clause (Correct answer)
- ORDER BY clause
Correct answer: JOIN clause
`$lookup` performs a left outer join from one collection to another, similar to SQL's JOIN.
Question 7: When using `mysql2` in Node.js with promise support, how do you import the promise-based API?
- const mysql = require('mysql2');
- const mysql = require('mysql2/promise'); (Correct answer)
- const mysql = require('mysql2').promises;
- import mysql from 'mysql2/async';
Correct answer: const mysql = require('mysql2/promise');
`mysql2/promise` is the dedicated entry point that exposes all methods as Promises, enabling async/await usage.
What is the purpose of `mongoose.connection.on('error', handler)` in a Node.js application?