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?
✗ Incorrect
The $regex operator matches strings against a regular expression pattern, useful for matching field names.
What does $objectToArray do in MongoDB aggregation?
✗ Incorrect
$objectToArray converts the fields of a document into an array of {k, v} objects, allowing pattern matching on keys.
Why might you use attribute patterns in MongoDB?
✗ Incorrect
Attribute patterns help find documents where field names change or vary, which is common in MongoDB.
Which of these is a valid regex pattern to match fields starting with 'user_'?
✗ Incorrect
/^user_/ matches strings that start with 'user_'.
In MongoDB, how do you check if any field name contains 'price' using aggregation?
✗ Incorrect
Converting fields to array and filtering keys with regex helps find fields containing 'price'.
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.