I have an array of employees that contains time slots of their meetings
[ {"id": 1, "name": "John", "meeting": [{"Time_Start": "1/3/2015 12:30:00 PM",
"Time_End": "1/3/2015 13:30:00 PM"}]},
{"id": 2, "name": "Peter", "meeting": [{"Time_Start": "1/5/2015 7:00:00 AM",
"Time_End": "1/5/2015 8:00:00 AM"}]}]
Considering that a work day is from 8 to 17.00. How can I find the available time slots for a meeting with specific duration for a day? Every meeeting starts on a half or whole hour.
You could use a boolean array where every index in the array represents a half an hour of the time slot during the day. And you could store these array's in an object where the day format is the key.
Related
In the User Document, I have registered users. I want to show the last 7 days' stats through API. 1st say- 7 users, 2nd day- 5 users, 3rd day- 3 users......
UserSchema contain- name, email, createdAt etc. How can I do that in Mongodb/mongoose?
sample data:
{
"_id": "612fa439ddb04331d6ea1e5d",
"name": "imran-1630512185253",
"createdAt": "2021-09-01T16:03:05.266Z",
},
{
"_id": "612fa439ddb04331d6ea1e5d",
"name": "imran-1630512185253",
"createdAt": "2021-08-31T16:03:05.266Z",
},
...........
expected output :
"filterData" : {
1st day: 3,
2nd day: 5,
3rd day: 3,
4th day: 8,
...
}
Use this API, I want to create a chart to show statistics. that API should provide which day, how many users were created.
You can do something like this
var d = new Date();
d.setDate(d.getDate()-7);
then
users.find({"createdAt": { $gt: d }})
The query you are describing is grouping a series of documents by day, when those documents contain UTC datetime.
MongoDB internally stores dates/times as the number of milliseconds since 1 Jan 1970, so the first step will need to be calculating the start-of-day for each document, then group by that start-of-day value.
If you are using MongoDB 5.0 you can use the $dateTrunc operator:
{$dateTrunc:{date:"$createdAt",unit:"day",timezone:"America/New_York"}}
For older versions, you can either calculate the date object that represents the start of day or build a string containing just the date.
For the string option:
{$concat: [
{$toString:{$year:{ date:"$createdAt", timezone:"America/New_York" }}},
"-",
{$toString:{$month:{ date:"$createdAt", timezone:"America/New_York" }}},
"-",
{$toString:{$dayOfMonth:{ date:"$createdAt", timezone:"America/New_York" }}},
]}
So overall the aggregation pipeline would be:
$match to select documents in the desired timeframe
$project to calculate the start of day for each document
$group by the start of day, counting the number of occurrences
further stages to convert the document into the desired format
I'm going to try to show you this without exposing customer data
Query:
Booking.objects.filter(startTime__year__gte=2020, startTime__month__gte=7,startTime__day__gte=27, depositPaid=True).order_by('startTime')
This should show all bookings from July 27, inclusive, onward into the future with no bounds. However, the bounds is on the 8th month. None of the bookings for August are showing.
If I run:
Booking.objects.filter(startTime__year__gte=2020, startTime__month__gte=8,startTime__day__gte=1, depositPaid=True).order_by('startTime')
Then I do get all the bookings in August. I don't understand why startTime__month__gte=7 doesn't show August.
Filtering by day__gte=27 will only return results where the day of the month is 27 or greater no matter what the month is, dates in August where the day is less than 27 will not be returned.
If you want to filter results after a certain date you should do it as a single query on the column, not as individual queries on the date parts.
import datetime
Booking.objects.filter(startTime__gte=datetime.date(2020, 7, 27), depositPaid=True).order_by('startTime')
I'm trying to get name and date of all the holidays but got this error "string indices must be integers"
import requests
import datetime
from datetime import date
url = 'https://api.alpha.ca.gov/StateHolidayCalendar/all'
resp = requests.get(url)
text = resp.text
print(text)
print('As of',date.today(), ', the remaining holidays in the US for this year are: ')
for h in text:
print ('holiday: ',h['name'], ' is on ',h['date'])
https://api.alpha.ca.gov/StateHolidayCalendar/all returns the following response:
[
{
"name": "New Year's Day",
"date": "2020-01-01T08:00:00.000Z"
},
{
"name": "Martin Luther King Jr. Day",
"date": "2020-01-20T08:00:00.000Z"
},
...
]
text is a string, hence h is a string, h['name'] and h['date'] make no sense at all.
You should parse the response using .json():
...
data = resp.json()
Then you can treat it as a list of dictionaries:
for holiday in data:
print(holiday['name'], holiday['date'])
will output
New Year's Day 2020-01-01T08:00:00.000Z
Martin Luther King Jr. Day 2020-01-20T08:00:00.000Z
Presidents' Day 2020-02-17T08:00:00.000Z
Cesar Chavez Day 2020-03-31T08:00:00.000Z
Memorial Day 2020-05-25T08:00:00.000Z
Independence Day 2020-07-04T08:00:00.000Z
Labor Day 2020-09-07T08:00:00.000Z
Veterans Day 2020-11-11T08:00:00.000Z
Thanksgiving Day 2020-11-26T08:00:00.000Z
Day after Thanksgiving 2020-11-27T08:00:00.000Z
Christmas Day 2020-12-25T08:00:00.000Z
New Year's Day 2021-01-01T08:00:00.000Z
Martin Luther King Jr. Day 2021-01-18T08:00:00.000Z
Presidents' Day 2021-02-15T08:00:00.000Z
Cesar Chavez Day 2021-03-31T08:00:00.000Z
Memorial Day 2021-05-31T08:00:00.000Z
Independence Day 2021-07-04T08:00:00.000Z
Labor Day 2021-09-06T08:00:00.000Z
Veterans Day 2021-11-11T08:00:00.000Z
Thanksgiving Day 2021-11-25T08:00:00.000Z
Day after Thanksgiving 2021-11-26T08:00:00.000Z
Christmas Day 2021-12-25T08:00:00.000Z
I am projecting a field as i want the difference between DateOfOcurrance and current time. Then I want to compare the diff_time field with one time which is in String format(HH:MM:SS) as I want those documents which have less than 15mins diff_time, but not getting.
{$project: {diff_time:{ $subtract: ["$DateOfOcurrance", new Date("2020-02-14")]}}},
{"$match": {"diff_time": { "$lt": "00:15:00" }}}
Dates are stored as the number of milliseconds since epoch. When you subtract one date from another, you get a NumberLong containing the difference in milliseconds.
To find differences less than 15 minutes, use:
{"$match": {"diff_time": { "$lt": 900000 }}}
(900000 = number of milliseconds in 15 minutes)
say I have a list of users and timestamps.
User Time
Tim 6:15PM
Tim 6:17PM
Tim 6:44PM
Tim 3:33PM
Bort 8:00PM
Bort 9:04PM
Bort 9:05PM
I want to count, for each user, the number of 30 minute increment they have a timestamp for, within each hour between 5PM and 8 30AM
Result:
Tim 2
Bort 2
Note that Tim has times at 6:15 and 6:17, this falls within the first 30 minutes of the 6PM hour, therefore it only counts as 1. He has a time at 6:44PM, which comes after 6:30PM, therefore it also counts as 1. Altogether he has 2
Bort has a time in the first half of 8PM but not in the latter half. So he gets 1. He then has two times within 9PM. Altogether he has 2.
Is this too intense to do in Powerpivot/dax?
I'm assuming you meant PM for those last two Bort rows since that's how you referred to it later.
I was able to achieve this by doing the following:
Create a calculated column HalfHour = ROUNDUP(48*TimeStamps[Time],0).
Create a measure CountTimeStamps as below.
Put them in a matrix visual with User on the rows and CountTimeStamps in the values.
CountTimeStamps =
CALCULATE(
DISTINCTCOUNT(TimeStamps[HalfHour]),
TimeStamps[Time] <= TIMEVALUE("8:30AM") ||
TimeStamps[Time] >= TIMEVALUE("5PM")
)