0
0
MongoDBquery~5 mins

Attribute pattern for variable fields in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an attribute pattern for variable fields in MongoDB?
It is a way to query documents where the field names can change or vary, using patterns like regular expressions to match those field names.
Click to reveal answer
intermediate
How can you match documents with field names starting with 'item_' in MongoDB?
Use the $regex operator on the field names with the pattern /^item_/. This helps find fields whose names start with 'item_'.
Click to reveal answer
beginner
Why do we need attribute patterns for variable fields in MongoDB?
Because MongoDB documents can have dynamic or varying field names, attribute patterns help us search or filter based on those changing field names.
Click to reveal answer
beginner
Which MongoDB operator helps to filter documents by matching field names with a pattern?
The $regex operator is used to match strings, including field names, against a regular expression pattern.
Click to reveal answer
advanced
Give an example of a MongoDB query to find documents where any field name contains 'score'.
Use an aggregation with $objectToArray to convert fields to key-value pairs, then filter keys with $regex: { $match: { $expr: { $gt: [ { $size: { $filter: { input: { $objectToArray: "$$ROOT" }, cond: { $regexMatch: { input: "$$this.k", regex: /score/ } } } } }, 0 ] } } }
Click to reveal answer
Which MongoDB operator is used to match field names with a pattern?
A$match
B$project
C$group
D$regex
What does $objectToArray do in MongoDB aggregation?
AFilters documents by array length
BConverts document fields into an array of key-value pairs
CSorts documents by field values
DUpdates array elements
Why might you use attribute patterns in MongoDB?
ATo query documents with dynamic or varying field names
BTo update all documents at once
CTo create indexes on fixed fields
DTo delete documents by ID
Which of these is a valid regex pattern to match fields starting with 'user_'?
A/user/
B/user$/
C/^user_/
D/^_user/
In MongoDB, how do you check if any field name contains 'price' using aggregation?
AUse $objectToArray and $filter with $regexMatch on keys
BUse $match with $exists
CUse $group by field names
DUse $project to rename fields
Explain how to query MongoDB documents when field names vary and you want to find fields matching a pattern.
Think about turning fields into an array to work with their names.
You got /4 concepts.
    Describe why attribute patterns are useful in MongoDB and give an example scenario.
    Consider when field names are not fixed but follow a naming style.
    You got /4 concepts.