All the objects that make up the Keep system are stored in a single MongoDB collection. Each object type is defined by the _t
array property. This array property enumerates all the types in the object model hierarchy that contribute to the definition of the specific document instance.
Let’s examine an EP1501
mercury controller with a very simple aggregation provided using the -s
command line argument.
keepcli aggregate -s '[{"$match":{"_t":"MercuryController"}},{"$limit":1},{"$project":{"_id":0,"_t":1}}]' -@prod | jq .
We see the object hiearchy clearly as first a KeepObject, then a Controller, MercuryController, and finally an Ep1501.
{
"_t": [
"KeepObject",
"Controller",
"MercuryController",
"Ep1501"
]
}
Knowing this information we can now develop a query to count the number of mercury controllers by type:
Step 1 - Filter by MercuryController
We’ll use the $match operator to filter by type Mercury Controller (which will get all the different specific mercury controller types)
{"$match":{"_t":"MercuryController"}}
Step 2 - Project out the derived type
We’ll now need to use the $project
operator and the $slice
operator to extract from the _t
property only the last value in the array.
{
"$project": {
"type": {"$slice": [ "$_t", -1] }
}
}
Step 3 - count by type
Step 2 projects a new object into the pipeline which just one property type This property will the be result of the $slice
function, which unfortunately is an array. We can use the $unwind to quickly turn that array (which will always have only one member) into a simple property. With the type property as a single value we can quickly get a count by type in sorted order using $sortByCount
{ "$unwind": "$type" },
{ "$sortByCount": "$type" }
run the script
We can run the script and find the number of controllers by type ([download controllerCountsByType.json](“/downloads/controllerCountsByType.json”))
keepcli aggregate -@prod < static/downloads/controllerCountsByType.json