0
0
MongoDBquery~20 mins

Election process concept in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Election Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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 } })
Adb.candidates.find({ votes: { $gt: 1000 } })
Bdb.candidates.find({ votes: { $lt: 1000 } })
Cdb.candidates.find({ votes: { $gte: 1000 } })
Ddb.candidates.find({ votes: 1000 })
Attempts:
2 left
💡 Hint
Use the $gt operator to find values greater than a number.
query_result
intermediate
2: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" } } }])
Adb.candidates.aggregate([{ $group: { _id: "$votes", totalVotes: { $count: {} } } }])
Bdb.candidates.aggregate([{ $sum: "$votes" }])
Cdb.candidates.aggregate([{ $group: { _id: null, totalVotes: { $avg: "$votes" } } }])
Ddb.candidates.aggregate([{ $group: { _id: null, totalVotes: { $sum: "$votes" } } }])
Attempts:
2 left
💡 Hint
Use $group stage with $sum accumulator to add values.
📝 Syntax
advanced
2: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 } })
ASyntaxError: Cannot use field name 'votes' directly in update document
BTypeError: votes is undefined
CNo error, query runs successfully
DRuntimeError: Invalid update operator
Attempts:
2 left
💡 Hint
In update documents, you cannot use field names as variables directly.
query_result
advanced
2: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)
Adb.candidates.aggregate([{ $sort: { votes: -1 } }, { $limit: 1 }])
Bdb.candidates.aggregate([{ $sort: { votes: 1 } }, { $limit: 1 }])
Cdb.candidates.find().sort({ votes: -1 }).limit(1)
Ddb.candidates.find().sort({ votes: 1 }).limit(1)
Attempts:
2 left
💡 Hint
Sort descending by votes and limit to 1 document.
🧠 Conceptual
expert
3: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?
AUse a compound index on <code>voterId</code> and <code>candidateId</code> without uniqueness
BCreate a unique index on the <code>voterId</code> field
CManually check for existing votes before inserting a new vote document
DAllow multiple votes and filter duplicates in application code
Attempts:
2 left
💡 Hint
Indexes can enforce uniqueness at the database level.