📖 Scenario: You are helping organize a small community election. You want to store information about candidates and votes in a MongoDB database.This project will guide you to create a collection for candidates, add a configuration for minimum votes to win, count votes for each candidate, and finally determine the winner.
🎯 Goal: Build a MongoDB setup that stores candidates, counts votes, and identifies the winner based on a minimum vote threshold.
📋 What You'll Learn
Create a candidates collection with candidate names and their initial votes.
Add a configuration variable min_votes_to_win to set the minimum votes needed to win.
Write a query to count votes for each candidate.
Write a query to find the candidate(s) who meet or exceed the minimum votes to win.
💡 Why This Matters
🌍 Real World
Community groups and organizations often use databases like MongoDB to manage election data efficiently.
💼 Career
Understanding how to store and query election data is useful for roles in data management, civic tech, and backend development.
Progress0 / 4 steps
1
Create the candidates collection with initial data
Create a MongoDB collection called candidates and insert these exact documents: { name: "Alice", votes: 10 }, { name: "Bob", votes: 15 }, and { name: "Charlie", votes: 7 }.
MongoDB
Hint
Use insertMany to add multiple documents to the candidates collection.
2
Add a minimum votes to win configuration
Create a variable called min_votes_to_win and set it to 12 to represent the minimum votes a candidate needs to win.
MongoDB
Hint
Use const to declare the variable min_votes_to_win with the value 12.
3
Query to count votes for each candidate
Write a MongoDB query using find() on the candidates collection to get all candidates with their names and votes.
MongoDB
Hint
Use find() with a projection to show only name and votes fields.
4
Find the winner(s) based on minimum votes
Write a MongoDB query using find() on the candidates collection to find candidate(s) where votes is greater than or equal to min_votes_to_win. Store the result in a variable called winners.
MongoDB
Hint
Use the $gte operator in the query filter to find candidates with votes greater than or equal to min_votes_to_win.
Practice
(1/5)
1. What is the main purpose of an election in a MongoDB replica set?
easy
A. To select a primary server that manages writes
B. To delete old data from the database
C. To backup the database automatically
D. To create new collections in the database
Solution
Step 1: Understand the role of election in MongoDB
In a replica set, election chooses a primary server to handle write operations.
Step 2: Identify the correct purpose
The primary server manages writes and coordinates data replication.
Final Answer:
To select a primary server that manages writes -> Option A
Quick Check:
Election = select primary server [OK]
Hint: Election picks the primary server for writes [OK]
Common Mistakes:
Thinking election deletes data
Confusing election with backup process
Believing election creates collections
2. Which MongoDB command shows the current election status in a replica set?
easy
A. db.showElection()
B. db.electionStatus()
C. rs.status()
D. rs.election()
Solution
Step 1: Recall the command to check replica set status
The command rs.status() shows detailed replica set info including election status.
Step 2: Verify other options
Other commands like db.showElection() or rs.election() do not exist in MongoDB.
Final Answer:
rs.status() -> Option C
Quick Check:
Check election with rs.status() [OK]
Hint: Use rs.status() to see election info [OK]
Common Mistakes:
Using non-existent commands
Confusing db commands with rs commands
Trying to use election-specific commands that don't exist
3. Given the following output from rs.status(), which member is currently the primary?