0
0
MongoDBquery~20 mins

Attribute pattern for variable fields in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Attribute Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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_?
A{ $expr: { $gt: [ { $size: { $objectToArray: '$$ROOT' } }, 0 ] } }
B{ $or: [ { 'attr_*': { $exists: true } } ] }
C{ $expr: { $gt: [ { $size: { $filter: { input: { $objectToArray: '$$ROOT' }, cond: { $regexMatch: { input: '$$this.k', regex: '^attr_' } } } } }, 0 ] } }
D{ attr: { $regex: '^attr_' } }
Attempts:
2 left
💡 Hint
Use $objectToArray to convert fields to key-value pairs and filter keys by regex.
📝 Syntax
intermediate
1: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_?
Adb.collection.find({ $expr: { $gt: [ { $size: { $filter: { input: { $objectToArray: '$$ROOT' }, cond: { $regexMatch: { input: '$$this.k', regex: '^data_' } } } } }, 0 ] } })
Bdb.collection.find({ $expr: { $gt: [ { $size: { $filter: { input: { $objectToArray: '$ROOT' }, cond: { $regexMatch: { input: '$$this.k', regex: '^data_' } } } } }, 0 ] } })
C)} } ] 0 ,} } } } } '_atad^' :xeger ,'k.siht$$' :tupni { :hctaMxeger$ { :dnoc ,} 'TOOR$$' :yarrAoTtcejbo$ { :tupni { :retlif$ { :ezis$ { [ :tg$ { :rpxe$ {(dnif.noitcelloc.bd
Db.collection.find({ $expr: { $gt: [ { $size: { $filter: { input: { $objectToArray: '$$ROOT' }, cond: { $regexMatch: { input: '$$this.k', regex: '^data_' } } } } }, 0 ] } })
Attempts:
2 left
💡 Hint
Check the variable referencing the root document inside $objectToArray.
🧠 Conceptual
advanced
2: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?
ABecause $objectToArray converts values to keys, allowing regex matching on values instead of keys.
BBecause MongoDB automatically indexes all fields starting with 'attr_', so $objectToArray optimizes the query.
CBecause $filter can only be used on arrays, and documents are arrays by default in MongoDB.
DBecause MongoDB does not support querying field names directly with regex, so fields must be converted to array elements to filter keys.
Attempts:
2 left
💡 Hint
Think about how MongoDB stores documents and how queries work on field names.
optimization
advanced
2: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?
AStore all variable fields inside a subdocument named 'meta' and query with dot notation like 'meta.field'.
BCreate a wildcard index on all fields and query with a regex on the field name.
CUse $objectToArray and $filter with $regexMatch inside an aggregation pipeline to filter documents.
DUse $where with JavaScript to iterate over fields and check if any field name starts with 'meta_'.
Attempts:
2 left
💡 Hint
Consider indexing and query performance on large collections.
🔧 Debug
expert
3: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?
AThe regex pattern is incorrectly wrapped in slashes, causing it to be treated as a string literal.
BThe variable '$$this.k' is invalid and should be '$$this.key'.
CThe $filter input should be '$ROOT' instead of '$$ROOT'.
DThe $size operator cannot be used inside $expr.
Attempts:
2 left
💡 Hint
Check how regex patterns are specified in MongoDB aggregation expressions.