Convert SQL to MongoDB - sql

Convert
DELETE FROM oshibka WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(date) > 900
to
db.oshibka.remove( { ???: { $gt: 900 } } )

db.oshibka.remove( { date : { $lt : <now-900> } } )
You just need to calculate the date that 900 before now and compare that to the field in you documents.

Related

How to disable a graphql filter if the variable is not set?

I am working on a page with configurable filters. The graphql query looks like:
query GetPlayers(
$offset: Int
$limit: Int
$skillIds: [uuid!] = null
$playerTypeIds: [Int!] = null
$availability: Int = null
$timezones: [String!] = null
$search: String = null
) {
player(
order_by: { total_xp: desc }
offset: $offset
limit: $limit
where: {
availability_hours: { _gte: $availability }
timezone: { _in: $timezones }
player_type_id: { _in: $playerTypeIds }
Player_Skills: { Skill: { id: { _in: $skillIds } } }
_or: [
{ username: { _ilike: $search } }
{ ethereum_address: { _ilike: $search } }
]
}
) {
id
}
}
I would like the default behavior to be to return all entries. I am using Hasura 1.3.3 and null is interpreted as {} which will return all entries. The only problem is the Player_Skills: { Skill: { id: { _in: $skillIds } } } line. There is a join table with foreign keys referring to the players and skills tables, and that line will only return players who have at least one entry in that table.
Is it possible to form a query that will ignore the skills filter if $skillIds is null?

Filtering by a subtotal of the measure

I have the following query in MDX:
SELECT {
[Season].[Season].members *
[Measures].[Wins]
} ON COLUMNS, {
Filter([Team].[Name].[Name].members, [Measures].[Wins] > 100)
} ON ROWS
FROM [Nfl2]
Here I am filtering by "All Wins > 100". How would I filter by "Wins in 2012 > 10" ?
Use this code :
SELECT {
[Season].[Season].members *
[Measures].[Wins]
} ON COLUMNS, {
Filter([Team].[Name].[Name].members, ([Season].[Season].&[2012],[Measures].[Wins]) > 100)
} ON ROWS
FROM [Nfl2]

How to change xAxisTickFormatting in ngx-charts-line-chart based on timeline selection?

By default, ticks are formatted based on time range selection in timeline. If it pans across days it shows month and if it is with in a day, it shows only time. This is great!
Now I want to localize these ticks. I could provide xAxisTickFormatting to get this done but I want to have the formatting based on the time range selection. "MMM DD" or "HH:MM" based on the current time range selection.
For this I need to change the formatting function dynamically on time range selection event. Is there such an event? Or is there any other way to achieve this?
In your chart, among the other attributes, you can declare
<ngx-charts-bar-horizontal-normalized
...
[xAxis]="true"
[xAxisTickFormatting]='formatPercent'
...
</ngx-charts-bar-horizontal-normalized>
formatPercent is a function declared in your .ts file (I'm using Angular) written like
formatPercent(val) {
if (val <= 100) {
return val + '%';
}
}
For any reference check the documentation here
Hope this helps.
It looks like, date is formatted based on the d3 logic. It uses the precision available to that tick. So if date is 12/15/2020 11:30:00, precision is at minute level. Similarly if date is 12/15/2020 00:00:00, precision is at day level.
Now we can choose the format options accordingly.
var locale = 'fr-FR'; // 'en-US'
function formatDate(value) {
let formatOptions;
if (value.getSeconds() !== 0) {
formatOptions = { second: '2-digit' };
} else if (value.getMinutes() !== 0) {
formatOptions = { hour: '2-digit', minute: '2-digit' };
} else if (value.getHours() !== 0) {
formatOptions = { hour: '2-digit' };
} else if (value.getDate() !== 1) {
formatOptions = value.getDay() === 0 ? { month: 'short', day: '2-digit' } : { weekday: 'short', day: '2-digit' };
} else if (value.getMonth() !== 0) {
formatOptions = { month: 'long' };
} else {
formatOptions = { year: 'numeric' };
}
return new Intl.DateTimeFormat(locale, formatOptions).format(value);
}
var dates = ['12/15/2020 11:30:30', '12/15/2020 11:30:00', '12/15/2020 11:00:00', '12/15/2020 00:00:00', '12/13/2020 00:00:00', '12/01/2020 00:00:00', '01/01/2020 00:00:00'];
for (date of dates) {
console.log(date, '=>', formatDate(new Date(date)));
}
Now this function can be used as
<ngx-charts-line-chart
[xAxis]="true"
[xAxisTickFormatting]="formatDate">
</ngx-charts-line-chart>

Azure Stream Analytics: Get Array Elements by name

I was wondering if it is possible for me to get the elements of the array by the name of property than the position. For example, this is my incoming data:
{
"salesdata": {
"productsbyzone": {
"zones": [{
"eastzone": "shirts, trousers"
},
{
"westzone": "slacks"
},
{
"northzone": "gowns"
},
{
"southzone": "maxis"
}
]
}
}
}
I intend to move this to a SQL database and I have columns within the database for each zone. The problem is that the order of different zones changes within each json. I was successfully using the following query until I realized that the position of the zones changes within each json:
WITH
salesData AS
(
SELECT
(c.salesdata.productsbyzone.zone,0) as eastzone,
(c.salesdata.productsbyzone.zone,1) as westzone,
(c.salesdata.productsbyzone.zone,2) as northzone,
(c.salesdata.productsbyzone.zone,3) as sourthzone,
FROM [sales-data] as c
)
SELECT
eastzone.eastzone as PRODUCTS_EAST,
westzone.westzone as PRODUCTS_WEST,
northzone.northzone as PRODUCTS_NORTH,
southzone.southzone as PRODUCTS_SOUTH
INTO PRODUCTSDATABASE
FROM salesData
Need a way to reference these fields by the name rather than by the position.
I recommend a solution: Use the JavaScript UDF in the azure stream job to complete the columns sort.
Please refer to my sample:
Input data(upset the order):
{
"salesdata": {
"productsbyzone": {
"zones": [{
"westzone": "slacks"
},
{
"eastzone": "shirts, trousers"
},
{
"northzone": "gowns"
},
{
"southzone": "maxis"
}
]
}
}
}
js udf code:
function test(arg) {
var z = arg;
var obj = {
eastzone: "",
westzone: "",
northzone: "",
southzone: ""
}
for(var i=0;i<z.length;i++){
switch(Object.keys(z[i])[0]){
case "eastzone":
obj.eastzone = z[i]["eastzone"];
continue;
case "westzone":
obj.westzone = z[i]["westzone"];
continue;
case "northzone":
obj.northzone = z[i]["northzone"];
continue;
case "southzone":
obj.southzone = z[i]["southzone"];
continue;
}
}
return obj;
}
You can define the order you want in the obj parameter
SQL:
WITH
c AS
(
SELECT
udf.test(jsoninput.salesdata.productsbyzone.zones) as result
from jsoninput
),
b AS
(
SELECT
c.result.eastzone as east,c.result.westzone as west,c.result.northzone as north,c.result.southzone as south
from c
)
SELECT
b.east,b.west,b.north,b.south
INTO
jaycosmos
FROM
b
Output:
Hope it helps you.
You can use GetArrayElement to return array element then access to each property. Please refer the below query
WITH
salesData AS
(
SELECT
GetArrayElement(zones,0) as z
FROM [sales-data] as s
)
SELECT
z.eastzone
z.westzone
z.northzone
z.southzone
FROM PRODUCTSDATABASE
FROM salesData

error it showing Trying to get property of non-object

$sensor = Yii::app()->zdb->createCommand("select * from tbl_sensor where stype=2 and nid=$nid order by timestamp desc limit 10")->queryAll();
if($sensor)
{ return $sensor->sdata/10; }
else
{ return null; }
QueryAll is return array. use Like this
foreach($sensor as $key => $row) {
$yourvar = $row['sdata'];
}
QueryAll will give you array of arrays of data..
To get all the columns:
$sensor = Yii::app()->zdb->createCommand("select * from tbl_sensor where stype=2 and nid=$nid order by timestamp desc limit 10")->queryAll();
if($sensor) {
foreach($sensor as $data_row) {
foreach($data_row as $data_key => $data_value) {
echo "$data_key: $data_value\n";
}
}
}
else {
return null;
}
To get just sdata:
$sdata_values = Yii::app()->zdb->createCommand("select sdata from tbl_sensor where stype=2 and nid=$nid order by timestamp desc limit 10")->queryColumn();
if($sdata_values) {
foreach($sdata_values as $sdata_value) {
echo $sdata_value / 10;
}
}
else {
return null;
}