I don’t have any written rules. Its is about aesthetics and about code readability. These are subjective. I guess, proper indentation is the simple way of saying it.
An aggregation stage is too long (vertically), this makes a stage more than a page length or with all operators and fields put together and too wide (horizontally). Both cases affect code readability. When the readability of code is affected, it in turn affects the maintainability.
So, what is the correct indentation? For example, the following are three samples of the same code with different indentation. The first and last are either too wide or too long to read clearly. The second sample, fits within a page and I see it is indented appropriately. A set of related code when it fits within a page and also properly indented is good readability, the eye can glance from top to bottom of the page and get the meaning of the code. If scrolling is involved it becomes awkward and then difficult.
Sample 1:
db.collection.aggregate([
{
$group: {
_id: "$workers.order",
order_avg: { $avg: { $subtract: [ "$workers.endAt", "$workers.startAt" ] } },
global_values: { $addToSet: { some_id: "$_id", duration: { $subtract: [ "$endAt", "$startAt" ] } } },
another_field: { ... ... ...}
}
}
])
Sample 2:
{
$group: {
_id: "$workers.order",
order_avg: {
$avg: {
$subtract: [ "$workers.endAt", "$workers.startAt" ]
}
},
global_values: {
$addToSet: {
some_id: "$_id",
duration: { $subtract: [ "$endAt", "$startAt" ] }
}
},
another_field: {
... ... ...
}
}
}
Sample 3:
{
$group: {
_id: "$workers.order",
order_avg: {
$avg: {
$subtract: [
"$workers.endAt",
"$workers.startAt"
]
}
},
global_values: {
$addToSet: {
some_id: "$_id",
duration: {
$subtract: [
"$endAt",
"$startAt"
]
}
}
},
another_field: {
...
...
...
}
}
}