Explore key concepts of Node.js database integration with MongoDB, PostgreSQL, and MySQL. This quiz covers basic connections, queries, data modeling, and understanding of core differences across these databases for efficient server-side development.
Which method is commonly used to establish a connection to a MongoDB database in a Node.js application?
Explanation: The connect() method is commonly used to establish a connection to MongoDB in a Node.js app. linkDB(), openDatabase(), and dbConnect() are not standard methods for connecting to MongoDB. Choosing the wrong method may result in connection errors.
If you want to use a relational database with strong schema enforcement in a Node.js project, which database should you select?
Explanation: PostgreSQL is known for its strong schema enforcement and is a relational database. MongoDB and CouchDB are considered NoSQL databases and offer flexible schemas, while Redis is primarily used for caching, not as a main relational database.
What is the default port number used when connecting to a MySQL database from Node.js?
Explanation: 3306 is the default port for MySQL. 5432 is used by PostgreSQL, 27017 is the default for MongoDB, and 8080 is a common web server port. Using the wrong port would result in connection failures.
Which format does MongoDB use to store JSON-like documents in a Node.js application?
Explanation: MongoDB stores JSON-like documents in the BSON format, which stands for Binary JSON. XML, CSV, and YAML are data serialization formats used in other contexts but are not used for MongoDB's internal storage.
What is the correct SQL statement to retrieve all records from the customers table in a PostgreSQL Node.js integration?
Explanation: The standard SQL statement 'SELECT * FROM customers;' retrieves all records. The other options are not valid SQL and would result in errors in both PostgreSQL and other relational databases.
Which Node.js library is commonly used for interacting with a MySQL database?
Explanation: The 'mysql' library is a common choice for connecting to MySQL in Node.js. 'node-mongo' and 'mongoose' are MongoDB libraries, while 'pg' is for PostgreSQL. Using the wrong library would not work with MySQL.
In a Node.js MongoDB project using an object modeling library, what do you define to enforce document structure?
Explanation: A schema is defined to set rules for MongoDB documents using object modeling tools. Blueprint, Layout, and Script are not terms used for this functionality and would not be recognized by modeling libraries.
Which approach helps prevent SQL injection when running queries in Node.js with a MySQL database?
Explanation: Parameterized queries help prevent SQL injection by keeping data separate from SQL code. Concatenating user input is insecure. Disabling authentication and using GET instead of POST do not address injection risks directly.
What is the main purpose of a primary key in a PostgreSQL or MySQL table?
Explanation: A primary key is used to uniquely identify every row in a relational database table. It does not store images, encrypt data, or permit duplicate rows. Only unique identification is the correct role.
When using Node.js to query MongoDB or PostgreSQL, what programming concept handles the non-blocking nature of I/O?
Explanation: Asynchronous callbacks (or promises, or async/await) are used in Node.js to handle non-blocking database operations. Synchronous loops and blocking threads go against Node.js's non-blocking nature, while infinite recursion is not related to database I/O.
Which SQL statement is used to insert a new record into a MySQL or PostgreSQL table?
Explanation: INSERT INTO is the correct SQL statement for adding new data to a table. The others—APPEND ROW, CREATE RECORD, and PUT ITEM—are not valid SQL commands and would cause errors.
What is the equivalent of a table in relational databases when using MongoDB in a Node.js app?
Explanation: In MongoDB, a collection groups documents similarly to how a table groups rows in a relational database. Field is akin to a column, tuple is a row, and structure is a generic term.
What SQL statement deletes rows from a customers table based on a condition in a Node.js-PostgreSQL setup?
Explanation: DELETE FROM ... WHERE ... is the correct SQL statement to delete data meeting a condition. REMOVE ROW, DESTROY, and DROP RECORD are not valid SQL statements and would result in errors.
Which database feature ensures that a group of database operations in MySQL or PostgreSQL either all succeed or all fail?
Explanation: Transactions guarantee atomicity, meaning all operations inside succeed or fail together. Triggers automate actions, views provide virtual tables, and indexes speed up searches, but none provide atomic guarantees like transactions.
In a MySQL or PostgreSQL table, which property automatically generates sequential unique numbers for new records?
Explanation: Auto-increment is a field property that automatically creates unique numbers for each new row. Foreign keys enforce relationships, composite keys combine columns for uniqueness, and timestamps record date and time.
Which statement best describes a difference between MongoDB and relational databases like MySQL or PostgreSQL?
Explanation: MongoDB uses flexible documents that do not require a fixed schema. MySQL supports various data types, not just unstructured text. PostgreSQL can handle JSON objects, and MongoDB does not use the rigid row-and-column model of relational databases.