So, I was messing around with MongoDB, and I'm trying to get the aggregation queries scaled correctly count()so that I can easily calculate the percentage of occurrence of certain values in a collection document.
I have a document with a structure like:
{
foo : 'bar',
moo : 'cow',
values : {
alpha : true,
beta : false,
gamma : false,
delta : true ... (many more)
}
}
Now I have several thousand of these documents, and I want to efficiently calculate the percentage of true (or percentage false) of all values in the object values(and in my case there are ~ 50). those. what percentage of alpha time is true, beta is true, etc.
I started naively with count(), but it looks like it only allows one request at a time, so that led me to this (using the PHP Mongo class, but its basically just a regular function count():
$array_of_keys = array('alpha', 'beta', 'gamma', 'delta'...);
for($i=0;$i<count($array_of_keys);$i++){
$array_of_keys = [...]
for($i=0;$i<count($array_of_keys);$i++){
$false = intval($collection->count(array($array_of_keys[$i]=>false)));
$true = intval($collection->count(array($array_of_keys[$i]=>true)));
}
( 100) 9 .
?