Docs:
[{
id: 111,
weight: 1
},{
id: 222,
},{
id: 333,
weight: -1
}]
I want to order the docs by weight if weight not exists treat its weight as 0
How can I do that ?
You can use function as mentioned here with default:
r.table(TABLE_NAME).orderBy(r.asc(function(doc) {
return doc("weight").default(0)
}));
Related
data
userId height
curry 1
curry 2
kd 3
kd 4
james 2
input: userIds=[curry,kd,james]
i want
curry 2
kd 4
james 2
So, i want one record with the highest height in each group.
Get!!!!
but I don't understand.
$sort must definition before $group ,
if not, the height is not the largest.
Why? Or i was wrong ?
users := []string{"curry", "kd", "james"}
pipe := mongo.Pipeline{
{
{Key: "$match", Value: bson.D{{Key: "userId", Value: bson.M{"$in": users}}}},
},
{
// must before $group
{Key: "$sort", Value: bson.D{{Key: "height", Value: -1}}},
},
{
{Key: "$group",
Value: bson.D{
{Key: "_id", Value: "$userId"},
{Key: "height", Value: bson.D{{Key: "$first", Value: "$height"}}},
},
},
},
//don't work !!!!!.the height is not the largest
//{
// {Key: "$sort", Value: bson.D{{Key: "height", Value: -1}}},
//},
}
I've got these streams
const site = of([
{ name: 'Joe', age: 30, tsUpdate: 5 },
{ name: 'Frank', age: 20, tsUpdate: 4 },
{ name: 'Ryan', age: 50, tsUpdate: 4 }
]);
const db = of([
{ name: 'Jonas', age: 30, tsUpdate: 4 },
{ name: 'Frank', age: 21, tsUpdate: 8 },
{ name: 'Calgor', age: 50, tsUpdate: 4 },
{ name: 'Joe', age: 30, tsUpdate: 4 },
]);
The goal is to have a single array with distinct value for
name but with the highest tsUpdate
So for instance
{ name: 'Joe', age: 30, tsUpdate: 5 }
{ name: 'Joe', age: 30, tsUpdate: 4 }
I want
{ name: 'Joe', age: 30, tsUpdate: 5 }
I am able to have distinct value for name property
but I don't know how to pick the highest value for tsUpdate
https://stackblitz.com/edit/rxjs-mhept2?devtoolsheight=60
This could be a way to solve it:
forkJoin(
[
site,
db
]
).pipe (
map(([site, db]) => [...site, ...db]),
// Emit each item individually
mergeAll(),
// Group by their name
groupBy(o => o.name),
// For each separate group, find the object with the highest `tsUpdate`
mergeMap(
grp$ => grp$.pipe(
reduce((acc, crt) => crt.tsUpdate > acc.tsUpdate ? crt : acc),
),
),
// At the end, collect all the objects in an array
toArray(),
).subscribe(console.log);
A groupBy's group is a Subject, and that's why we can use mergeMap afterwards. That subject will emit whenever it finds an object that matches with its key.
StackBlitz.
Taking a look at different examples on the documentation, I found that the only way to include multiple graphs (or lines) on a serial chart would be to do so by placing the data in one array as such:
[{
category: 1,
value1: .8,
value2: .64,
},
{
category: 2,
value1: .75,
value2: -.4,
}];
However, this is rather tedious if you have multiple data sets you are trying to display at once; is there an alternative way to do this, where you would pass multiple arrays at once (this is what I figured an implementation would look like, but it is not the case):
[
// First set of data
{ category: 0, value: .5},
{ category: 1, value: .5},
{ category: 2, value: .5},
{ category: 3, value: .5},
{ category: 4, value: .3},
{ category: 5, value: 1}
],
// Second set of data
[
{ category: 0, value: .5 },
{ category: 1, value: .3 },
{ category: 2, value: .25 },
{ category: 3, value: .6 },
{ category: 4, value: .79 },
{ category: 5, value: .81 }
]],
Any ideas on how this may be done? Or would I need to do switch to a different type of chart?
This isn't possible in the regular AmCharts JavaScript Charts library. The only supported format is a single array of objects with the values consolidated by category as you've noticed. You'll have to preprocess your data beforehand.
The AmCharts Stock Chart library supports separate arrays of data in the dataSets array, however it only supports date-based data.
AmCharts.makeChart("chartdiv", {
"type": "stock",
"dataSets": [{
// other properties omitted
"dataProvider": [{
category: "2017-08-01,
value: 3
}, {
category: "2017-08-02,
value: 2
}, {
category: "2017-08-03,
value: 1
}, // ...
]
}, {
// other properties omitted
"dataProvider": [{
category: "2017-08-01,
value: 10
}, {
category: "2017-08-02,
value: 9
}, {
category: "2017-08-03,
value: 5
}, // ...
]
},
// ...
]
// ...
});
You can see this in action in the any of the stock chart demos.
I have a multidimensional array looks like as below
{
patient: {
value: 66,
entry_date: "2017-01-17",
source: "self entered",
risk_level: nil,
risk_ranges: [{
level: "low",
is_active: false,
range_low: 80,
range_high: 139,
unit: "mg/dL"
}, {
level: "moderate",
is_active: false,
range_low: 140,
range_high: 199,
unit: "mg/dL"
}, {
level: "high",
is_active: false,
range_low: 200,
range_high: nil,
unit: "mg/dL"
}]
}
}
I want to find out if is_active is false in the risk_ranges array.
Of course I can access to patient.risk_ranges[0].is_active, patient.risk_ranges[1].is_active and patient.risk_ranges[2].is_active but wondering if there's a better way to write this.
if the is_active is false in all 3 objects in array then set patient.risk_level = high
How can I do that?
You can do something along the lines of
if patient.risk_ranges.all?{ |range| range.is_active == false}
patient.risk_level = "high"
end
Given an input that looks like this:
[
{
id: 11,
valueId: 22,
valueDescription: 'Some value',
referenceId: 33,
referenceDescription: 'Some reference',
groupId: 44,
groupDescription: 'Some group'
},
{
id: 55,
valueId: 66,
valueDescription: 'Another value',
referenceId: 77,
referenceDescription: 'Another reference',
groupId: 88,
groupDescription: 'Another group'
}
]
And a desired output of:
{
entities: {
types: {
"11": { id: 11, valueId: 22, referenceId: 33, groupId: 44 },
"55": { id: 55, valueId: 66, referenceId: 77, groupId: 88 },
},
values: {
"22": { id: 22, description: "Some value" },
"66": { id: 66, description: "Another value" },
},
references: {
"33": { id: 33, description: "Some reference" },
"77": { id: 77, description: "Another reference" },
},
groups: {
"44": { id: 44, description: "Some group" },
"88": { id: 88, description: "Another group" },
}
},
result: [ 11, 55 ]
}
I'm not sure how I'd define Schemas for my 4 entity types that would pull multiple fields off of the flattened root object and also rename them. I see there's the assignEntity argument I can pass which I think I would use for the renaming part, but I'm not sure how I would define a Schema to indicate that one flat object becomes four entities.