Challenge - 5 Problems
Election Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find candidates with more than 1000 votes
Given a collection
candidates with documents containing name and votes, which query returns all candidates with votes greater than 1000?MongoDB
db.candidates.find({ votes: { $gt: 1000 } })Attempts:
2 left
💡 Hint
Use the $gt operator to find values greater than a number.
✗ Incorrect
The $gt operator selects documents where the value of the field is greater than the specified value. Here, it returns candidates with votes more than 1000.
❓ query_result
intermediate2:00remaining
Count total votes for all candidates
Which aggregation pipeline correctly sums the
votes field for all candidates in the candidates collection?MongoDB
db.candidates.aggregate([{ $group: { _id: null, totalVotes: { $sum: "$votes" } } }])Attempts:
2 left
💡 Hint
Use $group stage with $sum accumulator to add values.
✗ Incorrect
The $group stage groups all documents (using _id:null) and sums the votes field to get total votes.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this update query
What error does this MongoDB update query produce?
db.candidates.updateOne({ name: "Alice" }, { $set: { votes: votes + 1 } })Attempts:
2 left
💡 Hint
In update documents, you cannot use field names as variables directly.
✗ Incorrect
The update document tries to increment votes by using 'votes + 1' directly, which is invalid syntax. MongoDB requires $inc operator for increments.
❓ query_result
advanced2:00remaining
Find the candidate with the highest votes
Which query returns the candidate document with the highest number of votes?
MongoDB
db.candidates.find().sort({ votes: -1 }).limit(1)Attempts:
2 left
💡 Hint
Sort descending by votes and limit to 1 document.
✗ Incorrect
Sorting by votes descending (-1) and limiting to 1 returns the candidate with the highest votes.
🧠 Conceptual
expert3:00remaining
Election process: Ensuring vote uniqueness
In a MongoDB collection
votes where each document records a voterId and candidateId, which approach best ensures that each voter can only vote once?Attempts:
2 left
💡 Hint
Indexes can enforce uniqueness at the database level.
✗ Incorrect
A unique index on voterId prevents inserting multiple documents with the same voterId, ensuring one vote per voter.