Challenge - 5 Problems
Attribute Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find documents with variable field names matching a pattern
Given a MongoDB collection
products where some documents have fields like attr_color, attr_size, and others have different attr_* fields, which query returns all documents that have at least one field starting with attr_?Attempts:
2 left
💡 Hint
Use $objectToArray to convert fields to key-value pairs and filter keys by regex.
✗ Incorrect
Option C converts the document fields to an array, filters keys starting with 'attr_', and checks if any exist. This matches documents with at least one 'attr_' field.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in a MongoDB query filtering variable fields
Which of the following MongoDB queries has a syntax error when trying to find documents with fields starting with
data_?Attempts:
2 left
💡 Hint
Check the variable referencing the root document inside $objectToArray.
✗ Incorrect
Option B uses '$ROOT' instead of '$$ROOT' which is invalid syntax for referencing the root document variable in aggregation expressions.
🧠 Conceptual
advanced2:00remaining
Understanding attribute pattern matching in MongoDB documents
Why is it necessary to use
$objectToArray and $filter to find documents with fields matching a pattern like attr_* in MongoDB?Attempts:
2 left
💡 Hint
Think about how MongoDB stores documents and how queries work on field names.
✗ Incorrect
MongoDB queries cannot directly apply regex on field names. Converting the document to an array of key-value pairs allows filtering keys by regex.
❓ optimization
advanced2:30remaining
Optimizing queries for variable attribute fields in MongoDB
Which approach is the most efficient to find documents with any field starting with
meta_ in a large MongoDB collection?Attempts:
2 left
💡 Hint
Consider indexing and query performance on large collections.
✗ Incorrect
Storing variable fields inside a subdocument allows indexing and efficient querying using dot notation, which is faster than scanning all fields or using JavaScript.
🔧 Debug
expert3:00remaining
Debugging a MongoDB query for variable attribute fields
A developer wrote this query to find documents with fields starting with 'info_':
db.collection.find({ $expr: { $gt: [ { $size: { $filter: { input: { $objectToArray: '$$ROOT' }, cond: { $regexMatch: { input: '$$this.k', regex: '/^info_/' } } } } }, 0 ] } })
Why does this query return no results even though matching documents exist?Attempts:
2 left
💡 Hint
Check how regex patterns are specified in MongoDB aggregation expressions.
✗ Incorrect
In MongoDB aggregation expressions, regex patterns should be strings without slashes. Using '/^info_/' treats it as a string literal, so no keys match.