The number of readers per controller can have an impact on overall system performance. Determining the number of readers per controller is a great application for the aggregation pipeline, and keepcli
We’ll create a new file to hold our aggregation pipeline query called readersPerController.json
.
[
{}
]
The first operation is to include in the pipeline only MercuryReader
objects. The type of object is identified in the _t
property so the $match operator will be:
{
"$match":{"_t":"MercuryReader"}
}
The controller that a reader is attached to is identified by a reference in the ObjectLinks
array marked with a Relation
value of Controller. Therefore we need to use $unwind operator on the ObjectLinks
array and then filter only the Controller ObjectLinks
{
"$unwind":"$ObjectLinks"
},
{
"$match":{"ObjectLinks.Relation":"Controller"}
}
The final two stages of the pipeline will be $group operators. The first group will count the number of readers per controller:
{
"$group":{"_id":"$ObjectLinks.LinkedObjectId","count":{"$sum":1}}
}
Now our pipeline looks something like this, showing the key value of the controller, and the number of readers attached.
{"_id":{"$oid":"5d30868833442c01a4a9eedd"},"count":1}
{"_id":{"$oid":"57e036ac92dacd0d6cc6b480"},"count":6}
Finally, value that we’re interested in for this exercise is the average number of readers per controller, and so a final $group
operator is required, this time using null as the grouping value, thereby grouping the entire pipeline in a single group.
{
"$group":{"_id":null, "average":{"$avg":"$count"}}
}
The completed aggregation pipeline file should look something like this:
[
{
"$match": {
"_t": "MercuryReader"
}
},
{
"$unwind": "$ObjectLinks"
},
{
"$match": {
"ObjectLinks.Relation": "Controller"
}
},
{
"$group": {
"_id": "$ObjectLinks.LinkedObjectId",
"count": {
"$sum": 1
}
}
},
{
"$group": {
"_id": null,
"average": {
"$avg": "$count"
}
}
}
]
Use keepcli to execute this aggregation pipeline passing the output to jq for easy formatting
keepcli aggregate < readersPerController.json | jq .