Data which relates to multiple Schemas in MongoDB? - sql

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.

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.

Sequelize.js : Sum with condition and group by

I'm a biginner using Sequelize.
I spend hours trying to get the following code runs but I can't
I quit some columns from the table for better understanding.
Finance table
I want to get this result:
[
{
'month': 11,
'year': 2021,
'commission_paid_sum': 2700,
'comission_notPaid_sum':2500,
},
{
'month': 12,
'year': 2021,
'commission_paid_sum': 0,
'comission_notPaid_sum':1000,
},
{
'month': 1,
'year': 2022,
'commission_paid_sum': 2000,
'comission_notPaid_sum':0,
},
]
I tried:
1- adding a attribute but I don't get how to add the "where/having condition" in the fn Sum
db.Finanzas.findAll({
attributes: ['mes', 'ano','is_paid',
[Sequelize.fn('sum', Sequelize.col('agents_commission')), 'commission_paid_sum'],
[Sequelize.fn('sum', Sequelize.col('agents_commission')), 'comission_notPaid_sum'],
],
group: [ 'month' , 'year'],
I've tried also using the literal feature, but in this, I wasn't able to split them by month/year in the result.
If you think that there was an alternative option, I'll be happy to heard about that.
Thanks in advance!
Alejandro
Try it.
const { literal } = require('sequelize');
const finances = await Finance.findAll({
group: ['month', 'year'],
attributes: [
'year',
'month',
[literal(`SUM(CASE WHEN "is_paid" THEN "agents_commission" ELSE 0 END)`), 'commission_paid_sum'],
[literal(`SUM(CASE WHEN "is_paid" THEN 0 ELSE "agents_commission" END)`), 'commission_notPaid_sum']
]
});

How count e.g. ice days over month/years

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.

Datatable sort plugin for day month year hour:minute

Is there a sort plugin for the date format
16 Jul 2014, 2:04 p.m.
18 Jul 2014, 11:54 a.m.
27 Jun 2014, 5:56 p.m.
The dates are currently sorted in the default format (16 Jul should be before 18 Jul but after 27 Jun).
This is my table definition:
$(document).ready(function() {
var dt = $("#files_table").dataTable({
"paging": false,
"defaultContent": "No Data",
"order": [[ 0, "desc" ]],
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 7, ]},
{ "sClass": "center", "aTargets": [ 7, ]},
{ "sType": "date-uk", "aTargets": [ 0, ]},
]
});
At the bottom where I specify the date type (current "date-uk"), none of the date formats at
http://datatables.net/plug-ins/sorting/ correspond to day name-of-month year hh:mm.
Is my only alternative to make my own data source sort function? It seems like quite a common format.
You can sort your dates without any additional plugin, only you have to put the timestamp of the date in data-order attribute.
Demo.
Documentation.

How does Raven DB SQL Replication handle saving arrays to SQL?

I am currently setting up SQL Replication to replicate our Raven DB documents into SQL for reporting purposes. So far everything has been working great. However, I am now trying to save a document that contains an array for days of the week.
This is how the document looks in Raven:
{
"ClientId": "clients/385",
"Description": "Test",
"IsOneOff": false,
"RecursEveryWeeks": 1,
"StartDate": "2013-03-19T00:00:00.0000000",
"TaskStartTime": "12:00:00",
"TaskDuration": 120,
"TaskEndTime": "14:00:00",
"AdditionalResources": false,
"AdditionalVisitType": "TestType",
"BillableTo": "Private",
"RecurrenceEndDate": "2013-04-30T00:00:00.0000000",
"DaysOfWeek": [
"Monday",
"Tuesday",
"Wednesday",
"Friday",
"Saturday"
]
}
In SQL Replication I have done the following:
sqlReplicate("AdditionalVisit", "AdditionalVisitId", {
ClientId: this.ClientId,
Description: this.Description,
IsOneOff: this.IsOneOff,
RecursEveryWeeks: this.RecursEveryWeeks,
StartDate: this.StartDate,
TaskStartTime: this.TaskStartTime,
TaskDuration: this.TaskDuration,
TaskEndTime: this.TaskEndTime,
AdditionalResources: this.AdditionalResources,
AdditionalVisitType: this.AdditionalVisitType,
BillableTo: this.BillableTo,
RecurrenceEndDate: this.RecurrenceEndDate,
DaysOfWeek: this.DaysOfWeek
});
All of this works fine when I leave DaysOfWeek out of the SQL Replication but causes the server to crash when I leave in.
How should this be done in SQL Replication so everything in the array is saved to a DaysOfWeek column in SQL?
I've not tested this, but it's along the lines of what you want...just add this to the end of your current script.
for (var i=0; i<this.DaysOfWeek.length; i++) {
var day = this.DaysOfWeek[i];
sqlReplicate('AdditionalVisit_DaysOfWeek', 'AdditionalVisitId', {
AdditionalVisitId: documentId,
DayOfWeek: day,
});
}
By the way there is currently a bug in the SqlReplication for RavenDb 2.1 where deletes won't be pushed through to SqlReplication, it's supposed to be fixed in 2.5 branch but there are still some other issues that need to be worked on for it to become usable.