={ SUM( 1 / COUNTIF( A1:A100, A1:A100 ) ) }
PS. For this to work, the range (e.g. A1:A100) can not contain blank cells.
PSS. This is an array function... press CTRL+ENTER once written for it to work.
2015-09-08
MongoDB - Group stages example with match, group and sort
db.COLLECTION.aggregate([
// 1. Filter (optional)
{$match:
{
FIELD_1 : { $in: [ 'VALUE_1', 'VALUE_2' ] }
}
}
// 2. Group number one
, {$group :
{
_id: {
"FIELD_TO_GROUP_BY_1_ALIAS" : "$FIELD_TO_GROUP_BY_1"
, "FIELD_TO_GROUP_BY_2_ALIAS" : "$FIELD_TO_GROUP_BY_2"
}
, AGGREGATION_ALIAS : {$sum : "$FIELD_TO_AGGREGATE"}
}
}// group 1
// 3. Match (filter on the results of the previous aggregation)
, {$match :
{
population: {$gt : NUMERIC_VALUE }
}
}// match
// 4. Group by
, {$group :
{
_id: {"FIELD_TO_GROUP_BY_ALIAS" : "$FIELD_TO_GROUP_BY" }
, AGGREGATION_ALIAS : { $avg : "$FIELD_TO_AGGREGATE" }
}
}// group 2
// 5. Sort descending
, {$sort :
{
FIELD_OR_ALIAS_TO_SORT_BY : -1
}
}//sort
]);
// 1. Filter (optional)
{$match:
{
FIELD_1 : { $in: [ 'VALUE_1', 'VALUE_2' ] }
}
}
// 2. Group number one
, {$group :
{
_id: {
"FIELD_TO_GROUP_BY_1_ALIAS" : "$FIELD_TO_GROUP_BY_1"
, "FIELD_TO_GROUP_BY_2_ALIAS" : "$FIELD_TO_GROUP_BY_2"
}
, AGGREGATION_ALIAS : {$sum : "$FIELD_TO_AGGREGATE"}
}
}// group 1
// 3. Match (filter on the results of the previous aggregation)
, {$match :
{
population: {$gt : NUMERIC_VALUE }
}
}// match
// 4. Group by
, {$group :
{
_id: {"FIELD_TO_GROUP_BY_ALIAS" : "$FIELD_TO_GROUP_BY" }
, AGGREGATION_ALIAS : { $avg : "$FIELD_TO_AGGREGATE" }
}
}// group 2
// 5. Sort descending
, {$sort :
{
FIELD_OR_ALIAS_TO_SORT_BY : -1
}
}//sort
]);
2015-09-04
MongoDB - How to aggregate distinct children field into a single field array ($addToSet)
db.COLLECTION_NAME_HERE.aggregate([
{$group:
{
_id: "$FIELD_TO_GROUP_BY_NAME_HERE"
, AGGREGATED_FIELD_ALIAS : { $addToSet : "$FIELD_TO_AGGREGATE" }
}
}
]);
{$group:
{
_id: "$FIELD_TO_GROUP_BY_NAME_HERE"
, AGGREGATED_FIELD_ALIAS : { $addToSet : "$FIELD_TO_AGGREGATE" }
}
}
]);
2015-09-03
MongoDB - Aggregate (average) grouping by multiple fields
db.COLLECTION_NAME.aggregate([
{$group :
{
_id : {
"FIELD_1_ALIAS" : "$FIELD_1_NAME"
, "FIELD_2_ALIAS" : "$FIELD_2_NAME"
}
, AGGREGATION_RESULT_FIELD_ALIAS : {$avg : "$FIELD_NAME"}
}
}
]);
{$group :
{
_id : {
"FIELD_1_ALIAS" : "$FIELD_1_NAME"
, "FIELD_2_ALIAS" : "$FIELD_2_NAME"
}
, AGGREGATION_RESULT_FIELD_ALIAS : {$avg : "$FIELD_NAME"}
}
}
]);
2015-09-02
MacOS - Executing/sending the contents of a file to an executable program
cat /path/filename.txt | executable_program_here
In this example, sending the contents of a .js file, to mongodb.
cat script.js | mongo
In this example, sending the contents of a .js file, to mongodb.
cat script.js | mongo
MongoDB - Basic group by with a sum
db.COLLECTION_NAME.aggregate([
{$group:
{
_id : "$FIELD_TO_GROUP_BY",
RESULTING_SUM_FIELD_ALIAS : { $sum : "$FIELD_TO_SUM"}
}
}
]);
{$group:
{
_id : "$FIELD_TO_GROUP_BY",
RESULTING_SUM_FIELD_ALIAS : { $sum : "$FIELD_TO_SUM"}
}
}
]);
Subscribe to:
Posts (Atom)