How count e.g. ice days over month/years - mongodb-query

I have a weather station with data over 14 years every 10min: like this
_id:60fbcf880000000000000000
datum:2021-07-24T08:30:00.000+00:00
temperature:19.5
Now I want to count per year and per month certain days.
Ice days (temperature is a 24h always below 0°),
winter days (temperature is at a time under 0°)
cold days (max temp <10°C)
hot days (max temp 25-30°C)
very hot days (max temp over 30°).
I have no real clue about the best query code for mongo.
I can group for days, but then I have the issue to count those certain days ($buckets?)
I come until the grouping:
{$group: {
_id: [{$year: '$datum'}, <br>
{$month: '$datum'}, <br>
{$dayOfMonth: '$datum'}], <br>
temp_avg: {$avg: '$tempAussen'}, <br>
temp_min: {$min: '$tempAussen'}, <br>
temp_max: {$max: '$tempAussen'}, <br>
}}
Result in a list with elements like:
_id:Array
0:2020
1:3
2:2
temp_avg:6.12
temp_min:0.7
temp_max:9.6
But now starts my problem: How to count the days for (e.g. 2020 month 3) with temperature <0° and the other days?

Your grouping is working fine. You just need one more extra $group by month to sum conditionally for each type of day. For example, you can do
{
$group: {
_id: {
"year": "$_id.year",
"month": "$_id.month",
},
winter_days_count: {
$sum: {
"$cond": {
"if": {
$lt: [
"$temp_min",
0
]
},
"then": 1,
"else": 0
}
}
}
...
to count the winter days in the month.
Here is the Mongo playground for your reference.

Related

I want to show occupied slots and available slots of interviewer from users table for a day and for remaining week by selecting date time

I am not an expert in writing postgresql queries, need some help in here.
I am using a tool to make a dashboard retool.com.
I have 2 tables users and interviewers. A user is an interviewer as well as interviewee. When users id is saved as interviewer_id in interviews table he is considered as interviewer, in the same way when the user id is saved as interviewee_id he is considered as interviewee. start_time column in interviews saves the interview start time. interviews_slots column in users table has the interviewer slots in json form.
see the sample data here.
`
{
"monday": [
{
"start-time": "09:00",
"end-time": "17:30"
}
],
"tuesday": [
{
"start-time": "09:00",
"end-time": "17:30"
}
],
"wednesday": [
{
"start-time": "09:00",
"end-time": "17:30"
}
],
"thursday": [
{
"start-time": "09:00",
"end-time": "17:30"
}
],
"friday": [
{
"start-time": "09:00",
"end-time": "17:30"
}
]
}
This is my sql statement.
SELECT users_tbl.id,
users_tbl.name,
(select name from users where id = interview_tbl.interviewee_id) as interviewee,
interview_tbl,start_time,
interview_tbl.duration,
interview_slots,
concat(users_tbl.interview_slots->'monday'->0->'start-time',' - ', users_tbl.interview_slots->'monday'->0->'end-time')
as Monday,
concat(users_tbl.interview_slots->'tuesday'->0->'start-time',' - ', users_tbl.interview_slots->'tuesday'->0->'end-time')
as Tuesday,
concat(users_tbl.interview_slots->'wednesday'->0->'start-time',' - ', users_tbl.interview_slots->'wednesday'->0->'end-time')
as Wednesday,
concat(users_tbl.interview_slots->'thursday'->0->'start-time',' - ', users_tbl.interview_slots->'thursday'->0->'end-time')
as Thursday,
concat(users_tbl.interview_slots->'friday'->0->'start-time',' - ', users_tbl.interview_slots->'friday'->0->'end-time')
as Friday,
concat(users_tbl.interview_slots->'saturday'->0->'start-time',' - ', users_tbl.interview_slots->'saturday'->0->'end-time')
as Saturday,
concat(users_tbl.interview_slots->'sunday'->0->'start-time',' - ',users_tbl.interview_slots->'sunday'->0->'end-time')
as Sunday
FROM users as users_tbl
JOIN interviews interview_tbl
ON users_tbl.id=interview_tbl.interviewer_id
AND interview_tbl.created_at >= CURRENT_DATE;
I want to query out the data based on the interview start_time
suppose a interview is set for Tuesday i.e 20th Dec at 10:00 am for 1 hour, i need to display a interviewer is available slots for Tuesday like
consider the sample data is used to display
Interviewer
Tuesday occupied slots
Available Slots
Batman
20th Dec at 10:00 am
9:00 - 10:00,
11:00 - 17:30.
Again in need to display similar data for the whole current week.
Thanks in advance.

CoinmarketCap API, 24h volume is greater then 7d volume, how is that possible?

I am trying to understand the data I get from CoinmarketCap API but I have a hard time figuring out how it is possible for the 24 hour volume to surpass the 7 days volume?
Example: Tesla tokenized stock Bittrex (TSLA), with ID: 7919
URL = https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id=7919&convert=USD&aux=volume_7d
As of right now the return data is:
"data": {
"7919": {
"id": 7919,
"name": "Tesla tokenized stock Bittrex",
"symbol": "TSLA",
"slug": "tesla-tokenized-stock-bittrex",
"last_updated": "2021-11-01T18:49:07.000Z",
"quote": {
"USD": {
"price": 1162.0225130926194,
"volume_24h": 24207.58836555,
"volume_7d": 16413.00204064,
"volume_change_24h": 6693.5029,
"percent_change_1h": -0.28201653,
"percent_change_24h": 4.429778,
"percent_change_7d": 15.46871256,
"percent_change_30d": 49.19294983,
"percent_change_60d": 59.53607702,
"percent_change_90d": 62.49791811,
"market_cap": 0,
"market_cap_dominance": 0,
"fully_diluted_market_cap": 0,
"last_updated": "2021-11-01T18:49:07.000Z"
}
}
}
}
As you can see, the volume for the past 24 hours is bigger then the volume for the past 7 days. How is that possible?
This example is not the only one. As of right now, I have found the following id's where the 24h volume surpass 7d volume: 6760, 3149, 4112, 6482, 5623, and 7919

MongoDB Aggregate $min not calculating

I am using the aggregate:
db.quantum_auto_keys.aggregate([
{$match: {table_name: 'PIZZA_ORDERS'}},
{
$group: {
_id: { onDate: { $dateToString: {format: "%Y-%m-%d", date: '$created_on', timezone: 'America/Los_Angeles'}}, table_name: '$table_name' },
min: { $min: '$last_number' }
}},
{$sort: {_id: 1}}
]);
It ignores the onDate grouping and returns the min for the collection where table_name = PIZZA_ORDERS.
When I use $max it calculates the maximum pizza orders by day. $count also returns the number of orders per day correctly.
How should I go about getting the minimum and maximum values via Aggregate or is there a different way to get that information from my collection?
I updated to MongoDB 4.4 and the table_name was not the correct group. Changing it to include another field got min calculation to be correct.

Can Azure Cosmos DB do this kind of query?

I have a JSON object stored in Azure Cosmos DB, and I'm seeing if there's a way to write workable queries doing basic things like Order By.
The structure looks something like :
[
{
"id":"id1",
"title":"test title",
"dataRecord":{
"version":1,
"dataRecordItems":[
{
"itemTitle":"item title 1",
"type":"string",
"value":"My First Title"
},
{
"itemTitle":"item number",
"type":"number",
"value":1
},
{
"itemTitle":"date",
"type":"date",
"value":"21/11/2019 00:00:00"
}
]
}
},
{
"id":"id2",
"title":"test title again",
"dataRecord":{
"version":1,
"dataRecordItems":[
{
"itemTitle":"item title 2",
"type":"string",
"value":"My Second Title"
},
{
"itemTitle":"item number",
"type":"number",
"value":2
},
{
"itemTitle":"date",
"type":"date",
"value":"20/11/2019 00:00:00"
}
]
}
]
I can use ARRAY_CONTAINS to find objects with a particular value, but I run into all kinds of issues if I try to sort by an the value of an object which has the title of "date".
So, as an example, I'd like to be able to say something like (pseudoish code here):
SELECT * FROM c WHERE
ARRAY_CONTAINS(c.dataRecord.dataRecordItems,
{"itemTitle":"item title 2", "value" : "My Second Title"}, true)
AND
ARRAY_CONTAINS(c.dataRecord.dataRecordItems,{"itemTitle":"item number", "value" : 2}, true)
ORDER BY < *** SOMEHOW GET THE DATE HERE from itemTitle = date ***
Then, in this simple case, I would everything returned, but ordered by date.
Obviously in the future I would be pulling out individual fields, but it's all kind of moot if I can't do the first part.
Just wondering if anyone has any great ideas.
Cheers!
You need to store the date in ISO 8601 format:
Year:
YYYY (eg 1997)
Year and month:
YYYY-MM (eg 1997-07)
Complete date:
YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a
second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
https://www.w3.org/TR/NOTE-datetime

Data which relates to multiple Schemas in MongoDB?

I have a front-end app which sometimes requests all the days of a month, sometimes the weeks of the year (more precisely: the days these weeks contain);
I thought of saving everything in mongodb like this:
{
month: 'Feb',
weeks: [
[{ day: 'Mo', etc. }, { day: 'Tu', etc. } ],
[ week2 here etc.] ], //etc.
]
},
{
month: 'Mar',
weeks: [
[{ day: 'Mo', etc. }, { day: 'Tu', etc. } ],
[ week2 here etc.] ], //etc.
]
}
... but this won't work for obvious reasons. Not every month has 4 weeks starting from Monday etc.
How would I realise something like this in MongoDB (roughly), so that my front-end app can request months (=an array of all the days of a month), but also weeks (= an array of 7 day-objects, starting from Monday)? I would really appreciate some hints here, as I am a novice.