Mongodb - how to aggregate with "bucket" - database

that's my dataset:
[
{_id: "628d54495995f1e6589675b3", age: 52, sex: 1, cp: 0, trestbps: 125,
chol: 212, fbs: 0, restecg: "1", thalach: 168, exang: "0", oldpeak: 1},
{_id: "628d54495995f1e6589675b4", age: 53, sex: 1, cp: 1, trestbps: 140,
chol: 203, fbs: 1, restecg: "0", thalach: 155, exang: "1", oldpeak: 3.1},
]
... and so on
I want to group for 2 classes of age (under 40 (included) and over 40 (not included)) and for each class to determine which value of cp is the most frequent.
cp can have four different values: 0, 1, 2, 3
I thought bucket could be the right operator, but I can't get it to work
Thanks for helping

Related

How to change a key-value pair of a hash in Ruby?

I have a query that returns an array and a hash.
How can I change the hash and add a new key-value pair to it: import_id: 1, cost: 0, or can I use a map method on the query?
The query is as follows.
name = Store.joins(:paid => :supply).group(:name).select("supply.name").where("stores.identifier IN (?) ", tids).pluck(:supply_id, :name)
The array is as follows.
[[258, "Square"], [245, "App"]]
When I convert it to a hash, it returns the following.
{258=>"Square", 245=>"App"}
The desired output is as follows.
{{supply_id: 258, name: "Square", import_id: 1, cost: 0}, {supply_id: 245, name: "App", import_id: 1, cost: 0}}
It appears the response is an array of [supply_id, name] pairs. You can easily destructure the array and restructure the data as hashes with proper symbolic keys.
array = [[258, "Square"], [245, "App"]]
array.map do |(id, name)|
{ supply_id: id, name: name, import_id: 1, cost: 0 }
end
# [{:supply_id=>258, :name=>"Square", :import_id=>1, :cost=>0}, ...]
Use #select instead of #pluck and call .as_json or .map(&:attributes)
Store.joins(:paid => :supply).group(:name)
.select("supply.name").where("stores.identifier IN (?) ", tids)
.select(:supply_id, :name).as_json
# [{supply_id: 258, name: "Square"}, {supply_id: 245, name: "App"}]
or
Store.joins(:paid => :supply).group(:name)
.select("supply.name").where("stores.identifier IN (?) ", tids)
.select(:supply_id, :name).map(&:attributes)
# [{supply_id: 258, name: "Square"}, {supply_id: 245, name: "App"}]
or you can construct Hash with {import_id: 1, cost: 0} appended
Store.joins(:paid => :supply).group(:name)
.select("supply.name").where("stores.identifier IN (?) ", tids)
.select(:supply_id, :name)
.map {|e| {supply_id: e.supply_id, name: e.name, import_id: 1, cost: 0} }
# [{supply_id: 258, name: "Square", import_id: 1, cost: 0}, {supply_id: 245, name: "App", import_id: 1, cost: 0}]
or
You can use Hash#merge during Hash generation step to include {import_id: 1, cost: 0}
hash.merge({import_id: 1, cost: 0})
# To achieve: {{supply_id: 258, name: "Square", import_id: 1, cost: 0}, {supply_id: 245, name: "App", import_id: 1, cost: 0}}

JS - group, map and flatten array of objects with arrays in them

Dears,
I really am not able to achieve following result:
{"2002":[1,2,3...]},
{"2003":[1,2,3,4...]},
...
I have following data (short example below):
{ "Year": "28-01-2020", "numbers": [10, 12, 20, 32, 35, 37] },
{ "Year": "03-10-2019", "numbers": [1, 6, 16, 19, 20, 30] },
{ "Year": "11-01-2018", "numbers": [14, 21, 25, 27, 30, 39] },
{ "Year": "11-08-2015", "numbers": [8, 16, 17, 18, 38, 46] },
I managed to use lodash _.groupBy to achieve following mid result:
[{…}]
0:
2000: Array(4)
0:
Year: "2000"
numbers: (7) [empty, 6, 25, 27, 37, 48, 49]
Year: "2000"
numbers: (7) [empty, 7, 12, 19, 30, 45, 49]
2: {Year: "2000", numbers: Array(7)}
2001: Array(104)
[0 … 99]
[100 … 103]
100: {Year: "2001", numbers: Array(7)}
101: {Year: "2001", numbers: Array(7)}
[0 … 99]
0: {Year: "2002", numbers: Array(7)}
1: {Year: "2002", numbers: Array(7)}
..
but i would like to have one object per year, with all numbers that appeared in sub arrays in this year
Could you please help me, i have tired ES6 map and for in loops, but non give me the proper result
Thank you in advance
This worked for me:
const slicedYear = results.map(elem => (elem = { Year: elem.Year.slice(elem.Year.length - 4), numbers: elem.numbers }));
const groupedResults = _.groupBy(slicedYear, 'Year');
for (let key in groupedResults) {
const selectedNumbers = { [key]: [groupedResults[key].map(elem => elem.numbers)].flat(2) };
}

angularjs chartjs legend undefined

Can anyone figure out why the legend is undefined?
here is a plunker https://plnkr.co/edit/uSbRpx5w7nHvGQ3OG3x2?p=preview
here is the code
$scope.chartData = {
data: [20, 20, 20, 20, 20, 20, 20],
labels: [
"Joe",
"Jason",
"Mark",
"Mike",
"Ryan",
"Sean",
"Stephanie"
],
series: ['Year 2016',]
};
i have made 2 examples. the top one works, the one below it doesn't
Any idea?
In charts the data-set must be an array of data, where the data in-turn is itself an array
i.e)data-set: [data_1, data_2, data_3], where data_1=[1,2,3,4,5]; data_2=[45,32,12,25,12];..
And the series represents the corresponds the data.
But here in your case the data-set is an array of numbers, which is why the legend becomes undefined.
$scope.chartData = {
data: [[20, 20, 20, 20, 20, 20, 20]],
labels: [
"Joe",
"Jason",
"Mark",
"Mike",
"Ryan",
"Sean",
"Stephanie"
],
series: ['Year 2016' ]
};

Creating array of JSON objects from multiple columns

I have the following dataset here
id
key
category
avg_time_1
avg_time_2
1
1
10
10
20
2
1
20
30
40
3
2
10
10
50
4
2
20
60
70
Sqlfiddle.
I want to create a query that the result will be as follow:
key
avg_time_1
avg_time_1
1
[{ "category": 10, "avg_time": 10},{ "category": 20, "avg_time": 20 }]
[{"category": 10, "avg_time": 20}, {"category": 20, "avg_time": 40}]
2
[{ "category": 10, "avg_time": 10},{ "category": 20, "avg_time": 60}]
[{"category": 10, "avg_time": 50}, {"category": 20, "avg_time": 70}]
The idea is just to re-present the values cols avg_time_1 and avg_time_2 in a different way, as jsonb values.
Use jsonb_build_object() to build prime objects and jsonb_agg() to aggregate them into a json array:
select
key,
jsonb_agg(jsonb_build_object('category', category, 'avg_time', avg_time_1)) as avg_time_1,
jsonb_agg(jsonb_build_object('category', category, 'avg_time', avg_time_2)) as avg_time_2
from data_to_agg_json
group by key
key | avg_time_1 | avg_time_2
-----+----------------------------------------------------------------------+----------------------------------------------------------------------
1 | [{"avg_time": 10, "category": 10}, {"avg_time": 30, "category": 20}] | [{"avg_time": 20, "category": 10}, {"avg_time": 40, "category": 20}]
2 | [{"avg_time": 10, "category": 10}, {"avg_time": 60, "category": 20}] | [{"avg_time": 50, "category": 10}, {"avg_time": 70, "category": 20}]
(2 rows)
Db<>Fiddle.

Exact $filter in AngularJS

I have this filter:
$http.get('data.json').then(function(response){
$scope.dataCopy = response.data;
$scope.detMaq = $filter('filter')(angular.copy($scope.dataCopy), {index:hielo, frecuencia:frec, prod_24_h_kg:peso }, false);
console.log($scope.detMaq);
});
And I'm building a few div's full of info with data from that data.json.
However, let's say that peso is 1.000, so in my filter I want only data that match completely to show up. This however it'll bring up data with 10.000 also, because the filter is not exact.
If I put true instead of false in the filter, the page will always be blank. even though it should bring information.
Am I missing something? Since it's a triple filter should I put 3 true's?
Here's the json.data
[{
"codigo": 21420,
"desc": "......",
"desc_n": "......",
"sistema": "DUCHAS",
"material_carroceria": "A.....",
"refrigerada": "AIRE",
"modelo_cubito_nombre": "ALFA",
"modelo_cubito_nombre_comercial": "",
"modelo_cubito_gr": 17,
"prod_24_h_kg": 22,
"almacen_cubitos_kg": "6",
"almacen_cubitos_cubitos": 400,
"ciclo_cubitos": 18,
"datos_comerciales_ciclo_cubitos": 18,
"compresor_cv": " 1/5",
"remota": null,
"potencia_w_1": 340,
"potencia_w_2": 270,
"voltaje": 220,
"frecuencia": 50,
"fases": "I",
"uk": null,
"gas_tipo": "R404a",
"gas_carga_gr": 180,
"mse_ancho": 350,
"mse_alto": 590,
"mse_fondo": 475,
"mse_alto_con_patas": "SIN PATAS",
"mce_ancho": 430,
"mce_alto": 720,
"mce_fondo": 540,
"peso_neto": 32,
"peso_bruto": 36,
"index": 99,
"enlace_catalogo": "..........",
"familia": ".........."
}, {
"codigo": 21421,
"desc": "...........",
"desc_n": "..........",
"sistema": "DUCHAS",
"material_carroceria": "ACERO INOXIDABLE AISI 304",
"refrigerada": "AGUA",
"modelo_cubito_nombre": "ALFA",
"modelo_cubito_nombre_comercial": "",
"modelo_cubito_gr": 17,
"prod_24_h_kg": 24,
"almacen_cubitos_kg": "6",
"almacen_cubitos_cubitos": 400,
"ciclo_cubitos": 18,
"datos_comerciales_ciclo_cubitos": 18,
"compresor_cv": " 1/5",
"remota": null,
"potencia_w_1": 340,
"potencia_w_2": 210,
"voltaje": 220,
"frecuencia": 50,
"fases": "I",
"uk": null,
"gas_tipo": "R404a",
"gas_carga_gr": 140,
"mse_ancho": 350,
"mse_alto": 590,
"mse_fondo": 475,
"mse_alto_con_patas": "SIN PATAS",
"mce_ancho": 430,
"mce_alto": 720,
"mce_fondo": 540,
"peso_neto": 32,
"peso_bruto": 36,
"index": 99,
"enlace_catalogo": "....",
"familia": "....."
}]

Resources