Bird
Raised Fist0
MongoDBquery~20 mins

Tables vs collections thinking in MongoDB - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
MongoDB Tables vs Collections Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the difference between tables and collections

In MongoDB, which statement best describes the difference between a table in SQL and a collection in MongoDB?

AA table and a collection are exactly the same, both store data in fixed schemas with rows and columns.
BA table stores data as JSON documents, while a collection stores data in rows and columns.
CA table stores data in rows and columns with a fixed schema, while a collection stores documents that can have varying fields and structures.
DA table is used only for temporary data, while a collection stores permanent data.
Attempts:
2 left
💡 Hint

Think about how flexible the data structure is in MongoDB compared to SQL.

query_result
intermediate
2:00remaining
Query result difference between SQL and MongoDB

Given a SQL table users with columns id and name, and a MongoDB collection users with documents having fields _id and name, what would be the result of the following MongoDB query?

db.users.find({ name: "Alice" })

Assuming the collection has these documents:

[{ _id: 1, name: "Alice" }, { _id: 2, name: "Bob" }, { _id: 3, name: "Alice", age: 30 }]
A[]
B[{ _id: 1, name: "Alice" }, { _id: 3, name: "Alice", age: 30 }]
C[{ _id: 2, name: "Bob" }]
D[{ id: 1, name: "Alice" }]
Attempts:
2 left
💡 Hint

The query filters documents where the name field equals "Alice".

📝 Syntax
advanced
2:00remaining
Identify the invalid MongoDB collection creation syntax

Which of the following commands will cause a syntax error when creating a collection in MongoDB?

Adb.createCollection("orders", { capped: true, size: 5242880 })
Bdb.createCollection("products")
Cdb.products.insertOne({ name: "Book", price: 12.99 })
Ddb.createCollection(products)
Attempts:
2 left
💡 Hint

Look for missing quotes or incorrect argument types.

optimization
advanced
2:00remaining
Optimizing data retrieval in collections vs tables

You have a large SQL table and a MongoDB collection both storing user data. Which approach is generally faster for retrieving a user by their unique ID?

AMongoDB's default _id index provides fast retrieval similar to SQL primary key indexes, so both are generally equally fast.
BMongoDB collections do not support indexes, so SQL tables are always faster.
CUsing an indexed primary key in SQL tables is generally faster than MongoDB's default _id index.
DMongoDB requires scanning the entire collection, so SQL tables are faster for unique ID lookups.
Attempts:
2 left
💡 Hint

Think about how both systems index unique identifiers.

🔧 Debug
expert
3:00remaining
Debugging schema design misunderstanding between tables and collections

A developer tries to enforce a fixed schema in a MongoDB collection by creating a table-like structure with all fields mandatory. Which problem will most likely occur?

AMongoDB allows flexible schemas, so documents missing some fields will be accepted, leading to inconsistent data.
BMongoDB will reject any document that does not have all fields, causing frequent insertion errors.
CMongoDB automatically fills missing fields with null values to enforce the schema.
DMongoDB converts the collection into a SQL table to enforce the schema.
Attempts:
2 left
💡 Hint

Consider MongoDB's schema flexibility and validation behavior by default.

Practice

(1/5)
1. Which of the following best describes a MongoDB collection compared to a SQL table?
easy
A. A collection cannot store nested data.
B. A collection stores flexible documents without fixed columns.
C. A collection is organized strictly in rows and columns.
D. A collection requires fixed columns and strict schema.

Solution

  1. Step 1: Understand MongoDB collections

    MongoDB collections store documents that can have different fields and structures.
  2. Step 2: Compare with SQL tables

    SQL tables require fixed columns and rows, unlike collections.
  3. Final Answer:

    A collection stores flexible documents without fixed columns. -> Option B
  4. Quick Check:

    Collections = flexible documents [OK]
Hint: Collections are flexible; tables have fixed columns [OK]
Common Mistakes:
  • Thinking collections require fixed columns
  • Assuming collections store data in rows and columns
  • Believing collections cannot have nested data
2. Which of the following is the correct way to create a collection named users in MongoDB?
easy
A. CREATE TABLE users;
B. CREATE COLLECTION users;
C. db.createCollection('users');
D. db.users.create();

Solution

  1. Step 1: Recall MongoDB syntax for creating collections

    MongoDB uses db.createCollection('name') to create collections.
  2. Step 2: Check other options

    Options A, B, and D use invalid MongoDB syntax.
  3. Final Answer:

    db.createCollection('users'); -> Option C
  4. Quick Check:

    Use db.createCollection('name') to create collections [OK]
Hint: Use db.createCollection('name') in MongoDB [OK]
Common Mistakes:
  • Using SQL CREATE TABLE syntax in MongoDB
  • Trying to call create() on collection object
  • Using CREATE COLLECTION which is invalid
3. Given a MongoDB collection products with documents like { name: 'Pen', price: 1.5 } and { name: 'Notebook', price: 3 }, what will db.products.find({ price: { $gt: 2 } }) return?
medium
A. All products with price greater than 2
B. All products with price less than 2
C. All products with price equal to 2
D. Syntax error

Solution

  1. Step 1: Understand the query filter

    The filter { price: { $gt: 2 } } selects documents where price is greater than 2.
  2. Step 2: Apply filter to example data

    Only the document with price 3 matches; price 1.5 does not.
  3. Final Answer:

    All products with price greater than 2 -> Option A
  4. Quick Check:

    $gt means greater than [OK]
Hint: Remember $gt means greater than in MongoDB queries [OK]
Common Mistakes:
  • Confusing $gt with $lt
  • Thinking it returns products with price less than 2
  • Assuming syntax error due to $gt usage
4. You tried to insert a document into a MongoDB collection with db.orders.insert({id: 1, item: 'Book'}) but got a deprecation warning. What is the likely cause?
medium
A. The insert method is deprecated; use insertOne or insertMany instead.
B. MongoDB requires documents to have a field named _id, not id.
C. The collection name must be plural, so 'orders' is invalid.
D. Documents cannot have string values in MongoDB.

Solution

  1. Step 1: Check MongoDB insert method usage

    The insert() method is deprecated in modern MongoDB versions.
  2. Step 2: Use correct insertion methods

    Use insertOne() or insertMany() to insert documents.
  3. Final Answer:

    The insert method is deprecated; use insertOne or insertMany instead. -> Option A
  4. Quick Check:

    Use insertOne/insertMany, not insert() [OK]
Hint: Use insertOne or insertMany, not insert() [OK]
Common Mistakes:
  • Thinking _id field is mandatory to insert
  • Believing collection names must be plural
  • Assuming string values are invalid in documents
5. You want to store user profiles where each user can have different sets of contact methods (email, phone, social media). Which is the best approach in MongoDB?
hard
A. Create separate tables for each contact method and join them.
B. Store all contact methods as a single string field.
C. Use a fixed schema collection with all possible contact fields, leaving some empty.
D. Use a single collection with flexible documents holding different contact fields.

Solution

  1. Step 1: Consider MongoDB's flexible schema

    MongoDB collections allow documents to have different fields, perfect for varying contact methods.
  2. Step 2: Compare with relational approach

    Relational tables require joins and fixed schemas, less flexible for this use case.
  3. Final Answer:

    Use a single collection with flexible documents holding different contact fields. -> Option D
  4. Quick Check:

    Flexible documents fit varying user data best [OK]
Hint: Flexible documents handle varied user data best [OK]
Common Mistakes:
  • Trying to use fixed schema for varying data
  • Using multiple tables and joins in MongoDB
  • Storing complex data as a single string