Discover how to keep your database neat even when your data keeps changing!
Why Attribute pattern for variable fields in MongoDB? - Purpose & Use Cases
Imagine you have a collection of products, and each product can have different sets of features. You try to create separate fields for every possible feature manually, but soon the list grows too long and messy.
Manually adding fields for every possible feature makes your database cluttered and hard to manage. It becomes slow to update, and you risk missing or duplicating data because the structure is rigid and inconsistent.
The attribute pattern lets you store variable fields as key-value pairs inside a single array or object. This way, you can flexibly add any feature without changing the database structure, keeping it clean and easy to query.
{ product1: { color: 'red', size: 'M', weight: '1kg' }, product2: { color: 'blue', size: 'L' } }{ product1: { attributes: [ { key: 'color', value: 'red' }, { key: 'size', value: 'M' }, { key: 'weight', value: '1kg' } ] }, product2: { attributes: [ { key: 'color', value: 'blue' }, { key: 'size', value: 'L' } ] } }This pattern enables you to handle any number of different fields dynamically, making your database flexible and scalable.
Online stores use this pattern to store product specifications that vary widely, like electronics with different technical features or clothes with various sizes and colors.
Manual fixed fields become messy with variable data.
Attribute pattern stores variable fields as key-value pairs.
It keeps data flexible, clean, and easy to manage.
Practice
Solution
Step 1: Understand pattern matching in MongoDB
MongoDB uses $regex to match string patterns inside field values.Step 2: Identify the operator for text pattern search
$regex is the operator designed for regular expression matching in queries.Final Answer:
$regex -> Option BQuick Check:
Pattern matching = $regex [OK]
- Confusing $regex with $match which filters documents
- Using $group which aggregates data, not filters by pattern
- Using $project which reshapes documents, not filters
Solution
Step 1: Understand field-to-array conversion
$objectToArray converts document fields into an array of {k, v} pairs.Step 2: Identify the correct stage for this conversion
$objectToArray is the aggregation operator that does this transformation.Final Answer:
$objectToArray -> Option AQuick Check:
Field to array = $objectToArray [OK]
- Using $filter which filters arrays but doesn't convert fields
- Confusing $arrayToObject which does the reverse conversion
- Using $match which filters documents, not fields
{
$project: {
filteredFields: {
$filter: {
input: { $objectToArray: "$ROOT" },
as: "field",
cond: { $regexMatch: { input: "$$field.k", regex: /^a/ } }
}
}
}
}Solution
Step 1: Convert document fields to array
$objectToArray converts {"a1":5,"b2":10,"c3":15} to [{k:"a1",v:5},{k:"b2",v:10},{k:"c3",v:15}].Step 2: Filter keys starting with 'a'
$filter with regex /^a/ keeps only fields where key starts with 'a', so only {k:"a1",v:5} remains.Final Answer:
{"filteredFields": [{"k": "a1", "v": 5}]} -> Option AQuick Check:
Filter keys /^a/ = only "a1" field [OK]
- Forgetting to convert fields to array before filtering
- Using wrong regex pattern or syntax
- Expecting all fields instead of filtered ones
{
$project: {
scores: {
$filter: {
input: { $objectToArray: "$ROOT" },
as: "field",
cond: { $regex: "score", input: "$$field.k" }
}
}
}
}Solution
Step 1: Check $regex usage inside $filter cond
$regex is a query operator, but inside aggregation expressions, $regexMatch must be used for regex matching.Step 2: Identify correct regex function for aggregation
Use $regexMatch with 'input' and 'regex' keys; $regex alone is invalid here.Final Answer:
Using $regex instead of $regexMatch inside $filter condition -> Option CQuick Check:
Aggregation regex = $regexMatch, not $regex [OK]
- Swapping input and regex keys in $regexMatch
- Trying to use $regex query operator inside aggregation
- Assuming $objectToArray can't be used on $ROOT
Solution
Step 1: Convert document fields to array for filtering
Use $objectToArray on "$ROOT" to get array of key-value pairs.Step 2: Filter keys starting with "score_" using $filter and $regexMatch
Apply $filter with condition $regexMatch on keys matching /^score_/.Step 3: Convert filtered array back to object
Use $arrayToObject to convert filtered array back to an object with only score fields.Final Answer:
{ $project: { scores: { $arrayToObject: { $filter: { input: { $objectToArray: "$ROOT" }, as: "f", cond: { $regexMatch: { input: "$$f.k", regex: /^score_/ } } } } } } } -> Option DQuick Check:
Filter keys + convert back = { $project: { scores: { $arrayToObject: { $filter: { input: { $objectToArray: "$ROOT" }, as: "f", cond: { $regexMatch: { input: "$$f.k", regex: /^score_/ } } } } } } } [OK]
- Trying to filter directly on $ROOT without conversion
- Using $match instead of $project for field transformation
- Not converting filtered array back to object with $arrayToObject
Ready to prove it?
Pass the test and this pattern moves unknown
