We use tables or collections to organize data. Understanding the difference helps us choose the right way to store and find information easily.
Tables vs collections thinking in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
Table: A structured set of rows and columns. Collection: A group of documents stored together in MongoDB.
Tables are used in SQL databases and have fixed columns.
Collections are used in MongoDB and store flexible documents.
Examples
MongoDB
SQL Table example: CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) );
MongoDB
MongoDB Collection example:
db.users.insertOne({
name: "Alice",
email: "alice@example.com",
age: 30
});Sample Program
This example shows how to add items to a MongoDB collection and then list them.
MongoDB
use mydatabase // Insert documents into a collection db.products.insertMany([ { name: "Pen", price: 1.5, color: "blue" }, { name: "Notebook", price: 3.0, pages: 100 } ]); // Find all products db.products.find().pretty();
Important Notes
Tables require a fixed structure; collections allow flexible fields per document.
Collections can store different shapes of data in the same group.
Think of tables like spreadsheets and collections like folders with different papers inside.
Summary
Tables are for structured, fixed data in rows and columns.
Collections hold flexible documents without fixed columns.
Choosing between them depends on how you want to organize and use your data.
Practice
1. Which of the following best describes a MongoDB collection compared to a SQL table?
easy
Solution
Step 1: Understand MongoDB collections
MongoDB collections store documents that can have different fields and structures.Step 2: Compare with SQL tables
SQL tables require fixed columns and rows, unlike collections.Final Answer:
A collection stores flexible documents without fixed columns. -> Option BQuick 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
Solution
Step 1: Recall MongoDB syntax for creating collections
MongoDB usesdb.createCollection('name')to create collections.Step 2: Check other options
Options A, B, and D use invalid MongoDB syntax.Final Answer:
db.createCollection('users'); -> Option CQuick 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
Solution
Step 1: Understand the query filter
The filter{ price: { $gt: 2 } }selects documents where price is greater than 2.Step 2: Apply filter to example data
Only the document with price 3 matches; price 1.5 does not.Final Answer:
All products with price greater than 2 -> Option AQuick 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
Solution
Step 1: Check MongoDB insert method usage
Theinsert()method is deprecated in modern MongoDB versions.Step 2: Use correct insertion methods
UseinsertOne()orinsertMany()to insert documents.Final Answer:
The insert method is deprecated; use insertOne or insertMany instead. -> Option AQuick 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
Solution
Step 1: Consider MongoDB's flexible schema
MongoDB collections allow documents to have different fields, perfect for varying contact methods.Step 2: Compare with relational approach
Relational tables require joins and fixed schemas, less flexible for this use case.Final Answer:
Use a single collection with flexible documents holding different contact fields. -> Option DQuick 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
