Node.js Databases: MongoDB, PostgreSQL, and MySQL Quiz Quiz

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.

  1. Connecting to MongoDB from Node.js

    Which method is commonly used to establish a connection to a MongoDB database in a Node.js application?

    1. linkDB()
    2. dbConnect()
    3. openDatabase()
    4. connect()

    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.

  2. Choosing a Relational Database

    If you want to use a relational database with strong schema enforcement in a Node.js project, which database should you select?

    1. PostgreSQL
    2. MongoDB
    3. CouchDB
    4. Redis

    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.

  3. Default Port of MySQL

    What is the default port number used when connecting to a MySQL database from Node.js?

    1. 3306
    2. 5432
    3. 8080
    4. 27017

    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.

  4. Data Format in MongoDB

    Which format does MongoDB use to store JSON-like documents in a Node.js application?

    1. CSV
    2. XML
    3. BSON
    4. YAML

    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.

  5. Basic SQL Query Example

    What is the correct SQL statement to retrieve all records from the customers table in a PostgreSQL Node.js integration?

    1. GET ALL customers;
    2. FIND customers ALL;
    3. SELECT * FROM customers;
    4. SHOW customers;

    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.

  6. Node.js Library for MySQL

    Which Node.js library is commonly used for interacting with a MySQL database?

    1. pg
    2. node-mongo
    3. mongoose
    4. mysql

    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.

  7. Schema Definition in MongoDB

    In a Node.js MongoDB project using an object modeling library, what do you define to enforce document structure?

    1. Blueprint
    2. Schema
    3. Layout
    4. Script

    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.

  8. Preventing SQL Injection

    Which approach helps prevent SQL injection when running queries in Node.js with a MySQL database?

    1. Concatenating user input into SQL
    2. Using parameterized queries
    3. Disabling authentication
    4. Using GET instead of POST

    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.

  9. Primary Key Concept

    What is the main purpose of a primary key in a PostgreSQL or MySQL table?

    1. To uniquely identify each record
    2. To allow duplicate rows
    3. To store images
    4. To encrypt data

    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.

  10. Async Operations with Databases

    When using Node.js to query MongoDB or PostgreSQL, what programming concept handles the non-blocking nature of I/O?

    1. Asynchronous callbacks
    2. Blocking threads
    3. Synchronous loops
    4. Infinite recursion

    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.

  11. Adding a New Record in SQL

    Which SQL statement is used to insert a new record into a MySQL or PostgreSQL table?

    1. APPEND ROW
    2. CREATE RECORD
    3. PUT ITEM
    4. INSERT INTO

    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.

  12. MongoDB Collection vs Table

    What is the equivalent of a table in relational databases when using MongoDB in a Node.js app?

    1. Collection
    2. Field
    3. Tuple
    4. Structure

    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.

  13. SQL Command to Delete Data

    What SQL statement deletes rows from a customers table based on a condition in a Node.js-PostgreSQL setup?

    1. DROP RECORD FROM customers;
    2. DESTROY customers;
    3. REMOVE ROW customers;
    4. DELETE FROM customers WHERE ...;

    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.

  14. Atomic Transactions

    Which database feature ensures that a group of database operations in MySQL or PostgreSQL either all succeed or all fail?

    1. Indexes
    2. Transactions
    3. Triggers
    4. Views

    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.

  15. Auto-Increment Field

    In a MySQL or PostgreSQL table, which property automatically generates sequential unique numbers for new records?

    1. Timestamp
    2. Auto-increment
    3. Foreign key
    4. Composite key

    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.

  16. NoSQL vs Relational Data Modeling

    Which statement best describes a difference between MongoDB and relational databases like MySQL or PostgreSQL?

    1. MongoDB stores data in flexible, schema-less documents
    2. PostgreSQL cannot handle JSON objects
    3. MySQL supports only unstructured text data
    4. MongoDB organizes data strictly in rows and columns

    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.