Your blogging app needs to generate a report showing the number of posts per tag for January 2025. Based on the lecture’s examples, which database and query approach would handle this efficiently?
SQL, but you’d need to manually loop through each post in TypeScript to count tags.
NoSQL with a MongoDB query: db.posts.find({ tags: "tech", date: { $gte: "2025-01-01" } });
db.posts.find({ tags: "tech", date: { $gte: "2025-01-01" } });
SQL with a query like: SELECT t.name, COUNT(p.id) FROM posts p JOIN post_tags pt ON p.id = pt.post_id JOIN tags t ON t.id = pt.tag_id WHERE p.date >= '2025-01-01' AND p.date < '2025-02-01' GROUP BY t.name;
SELECT t.name, COUNT(p.id) FROM posts p JOIN post_tags pt ON p.id = pt.post_id JOIN tags t ON t.id = pt.tag_id WHERE p.date >= '2025-01-01' AND p.date < '2025-02-01' GROUP BY t.name;
NoSQL, storing tags as a separate collection and joining them manually in code.
Either SQL or NoSQL would work equally well without any extra effort.
In the lecture’s Next.js app, the SQL code uses this query to fetch posts by tag:
SELECT p.id, p.title, p.content, p.date FROM posts p JOIN post_tags pt ON p.id = pt.post_id JOIN tags t ON t.id = pt.tag_id WHERE t.name = $1
What happens if the post_tags table is empty?
post_tags
The TypeScript function getPostsByTag crashes due to an invalid result.
getPostsByTag
The query returns posts with no tags only.
The query returns no posts because the JOIN finds no matches.
JOIN
The query throws an error due to missing relationships.
The query returns all posts regardless of tags.
Imagine your blogging app grows, and users demand a new feature: embedding images in posts. Which database setup from the lecture would handle this change with the least rework?
NoSQL, adding an images field to the post document like: "images": ["url1", "url2"].
images
"images": ["url1", "url2"]
SQL, creating a new post_images table to link images to posts.
post_images
SQL, adding an images column to the posts table with a TEXT type for image URLs.
posts
NoSQL, but you’d need to create a separate collection for images and link them manually.
You’re prototyping the blogging app quickly for a demo. Based on the lecture, which database choice and reasoning make the most sense?
SQL, because predefined schemas ensure data consistency from the start.
NoSQL, because you can store posts with tags in one document and skip table setup.
SQL, since the getPostsByTag function is faster with joins.
NoSQL, because it scales better for a demo with fake data.
Look at this NoSQL code from the lecture:
async function getPostsByTag(tag: string): Promise<Post[]> { await client.connect(); const db = client.db('blog_db'); const posts = await db.collection('posts').find({ tags: tag }).toArray(); return posts as Post[]; }
Your app now needs posts with multiple tags (e.g., "tech" AND "intro"). Which modification works?
Change the query to .find({ tags: ["tech", "intro"] }).
.find({ tags: ["tech", "intro"] })
Use .find({ tags: { $all: ["tech", "intro"] } }).
.find({ tags: { $all: ["tech", "intro"] } })
Rewrite the function to loop through posts in TypeScript and filter manually.
Chain multiple .find() calls like .find({ tags: "tech" }).find({ tags: "intro" }).
.find()
.find({ tags: "tech" }).find({ tags: "intro" })
Your blogging app’s admin wants to filter posts by date range and tag, like "all tech posts from January 2025." Which database(s) from the lecture can handle this easily?
SQL, with a query joining tables and filtering by date and tag.
NoSQL, with a query like db.posts.find({ tags: "tech", date: { $gte: "2025-01-01", $lte: "2025-01-31" } }).
db.posts.find({ tags: "tech", date: { $gte: "2025-01-01", $lte: "2025-01-31" } })
NoSQL, but you’d need to store tags separately and join them in code.
SQL, but only if you add extra indexes to the posts table.
This content is only available to registered users.Please register at the the home page.