Generate every odd days in specified period - node.js

Technology: NodeJS.
Problem: "how do I prepare the meeting date every odd or even days for the specified period in this way"
Thanks in advance, it would help me a lot if you could give me an example or direction on this.
let create = {
start_date: "01-02-2023",
end_date: "01-06-2023",
start_time: "15:00",
end_time: "19:00",
days: "even",
};
let meetings = [
{
date: "01-01-2023",
start__time: "14:00",
end__time: "16:00",
},
{
date: "03-01-2023",
start__time: "14:00",
end__time: "16:00",
},
{
date: "05-01-2023",
start__time: "14:00",
end__time: "16:00",
},
];
I tried some node packages but I couldn't produce the desired result.

Related

how to store hh(hour):mm(minutes) time in mongodb

Currently I am making scheduling system and storing information in mongodb
partial of my model looks like this
{
timings: [{from: "00:00", to: "01:00"}],
weekend: [0, 5, 6]
}
I am not sure if this will be good in the long run
can you please help me decide how to better store time in my documents
MongoDB does not have any time/interval data type, so you have to build your own solution. Some general notes:
Use week days according to ISO-8601 standard, i.e. first day (1) of week is Monday. Then it will be easier to create a Date value with $dateFromParts. For hour values use always 24-hour format.
You may consider to store times as
{from: {hour: 0, minute: 0}, to: {hour: 13, minute: 0}}
Otherwise, when you have to create Date value (e.g. for comparison), then it would be:
{
$dateFromParts : {
...,
hour: { $toInt: { $arrayElemAt: [ { $split: [ "$from", ":" ] }, 0 ] } },
minute: { $toInt: { $arrayElemAt: [ { $split: [ "$from", ":" ] }, 1 ] } }
}
}
Compared to
{
$dateFromParts : {
...,
hour: "$from.hour",
minute: "$from.minute"
}
}
Another approach is to store real Date, e.g. 0000-01-01T13:00:00 or any other arbitrary day value. When you use such values, simply ignore the date part.
Or you store number of minutes (0..1'440) or seconds (0..86'400) from midnight. However, such numbers are not user-friendly to read.

How to get date from object stored in mongodb?

I'm making an application with chats and posts, and in the chat, I want to show the time of the most recent post.
The date part of my mongoose schema for posts is below, as well as a picture of what my Mongodb date field looks like.
I'm trying to get the date to the front end, but am unsure about how to format it. I want to format it as yesterday at 12:45pm or 6 days ago, etc.
Any help is very much appreciated.
Mongoose Schema field for date:
date: {
type: Date,
default: Date.now
}
Mongodb data field
Mongo (and almost? no other database in the world) stores constants like "yesterday" or "last week".
The problem with these concepts like "yesterday" is that it's very semantic. if it's 00:01 is yesterday 2min ago? if the answer is yes you will actually have to update your database every minute if you're willing to compromise to look at time difference you will still have to do it every day.
I'm not sure what your actual business needs that make you want to do this. but I recommend you do it whilst fetching documents. otherwise this is not scaleable.
Here is a quick example on how to do this:
db.collection.aggregate([
{
"$addFields": {
currDay: {
"$dayOfMonth": "$$NOW"
},
dateDay: {
"$dayOfMonth": "$date"
},
dayGap: {
"$divide": [
{
"$subtract": [
"$$NOW",
"$date"
]
},
86400000/**miliseconds in a day*/
]
}
}
},
{
$addFields: {
date: {
"$switch": {
"branches": [
{
"case": {
$and: [
{
$lt: [
"$dayGap",
1
]
},
{
$eq: [
"$dateDay",
"$currDay"
]
}
]
},
"then": "today"
},
{
"case": {
$lt: [
"$dayGap",
2
]
},
"then": "yesterday"
},
{
"case": {
$lt: [
"$dayGap",
1
]
},
"then": "today"
}
],
default: {
"$concat": [
{
"$toString": {
"$round": "$dayGap"
}
},
" days ago"
]
}
}
}
}
}
],
{
allowDiskUse: true
})
MongoPlayground
As you can see you'll have to manually construct the "phrase" you want for every single option. You can obviously do the same in code I just choose to show the "Mongoi" way as I feel is more complicated.
If you do end up choosing updating your database ahead of time you can use the same pipeline combined with $out to achieve this.
One final note is that I cheated a little as this aggregation looks at the miliseconds difference only (apart from today field). meaning if it's 1AM then 50 hours ago. even though the date is "three" days ago will still show up as two days ago.
I hope this example shows you why this formatting is not used anywhere and the difficulties it brings. Mind you I haven't even brought up timezones concepts like "yesterday" are even more semantic for different regions.
In my option the only viable "real" solution is to build a custom function that does this in code. mind you this is not so much fun as you have to account for events like gap years, timezones, geographical zone and more, however it is doable.

Mongoose.countDocuments is not working when counting documents by moment js that are created last year

I am working on an open-source library called wertik-js and it uses Graphql, I have added reporting type helpers that return counting of mongoose models based on created at. Here in this case I have reportings like, total_count, total_added_last_year etc, total_added_today etc, For today I have filtered in this way:
model.countDocuments(
{
created_at: {
$gt: moment().startOf("day"),
$lt: moment().endOf("day"),
},
},
function (err, count) {
resolve(count);
}
);
And it works pretty fine. When using for last year, I have data created last year. Which counts in this way for last year
model.countDocuments(
{
created_at: {
$gt: moment().subtract(1, "year").startOf("year"),
$lt: moment().subtract(1, "year").endOf("year"),
},
},
function (err, count) {
resolve(count);
}
);
But last year count is not working and every time it returns 0 but I am sure I have written the query in right way and it returns the wrong number which is 0. Where total_added_today returns fine.
To reproduce the bug, you can pull this branch https://github.com/Uconnect-Technologies/wertik-js/tree/188-graphql-or-rest-api-enpoint-to-show-all-stats and:
Install packages and run the server sudo yarn && sudo yarn dev
Go to http://localhost:4000/
And execute the graphql query:
{
roleStats {
total_count
total_added_this_month
total_added_this_week
total_added_last_7_days
total_added_today
total_added_last_month
total_added_last_90_days
total_added_last_year
total_added_this_year
}
}
And you will be able to reproduce the error.
You can check this file https://github.com/Uconnect-Technologies/wertik-js/blob/188-graphql-or-rest-api-enpoint-to-show-all-stats/src/framework/reporting/index.ts#L110 from line 110 mongoose starts.
What I am doing wrong here?
Any help will be considered as a contribution to this open-source project.
Edit
An example document I am searching:
{
"_id": {
"$oid": "5ed85b8893280c021c2ddfd6"
},
"name": "John",
"created_at": "Thu Jun 04 2019 07:25:12 GMT+0500 (Pakistan Standard Time)",
"updated_at": "Thu Jun 04 2019 07:25:12 GMT+0500 (Pakistan Standard Time)",
"__v": 0
}
The problem is that the created_at field in the document contains a string: "Thu Jun 04 2019 07:25:12 GMT+0500 (Pakistan Standard Time)"
This is not sortable by date-time, or matchable with any date-range operators.
You might try converting those to dates with aggregation or update:
db.collection.update({created_at: {$type: "string"}},
[{$set: {created_at: {$toDate: {"$created_at"}}],
{multi:true})
Then you will be able to perform $gt or $lt tests meaningfully.
I think the problem is in this line
$lt: moment().subtract(1, "year").endOf("year"),
it should be
$lt: moment().endOf("year"),

Integer valued to 0 not present in output using GRPCv3

I am using GRPCv3 on node.js
I have a .proto file with the following message:
message ProductAvailabilityWithRatesResponse {
// How many slots are available for this day/time. Mandatory.
int32 capacity = 1;
// Date for when this product is available. Mandatory.
Date date = 2;
// When does this event start? Unset if this product does not support times.
Time time = 3;
// When does a pickup for this product start? Unset if this product does not support times or pickups.
Time pickupTime = 4;
// Rates with prices. Mandatory (should at least have one entry).
repeated RateWithPrice rates = 5;
}
on the server using console.log i see this output:
{ capacity: 1,
date: { year: 2019, month: 7, day: 1 },
rates: [ { rateId: 1, pricePerPerson: [Object] } ],
time: { hour: 9, minute: 0 } }
and on a client using node.js too:
{ rates:
[ { rateId: '1',
pricePerPerson: [Object],
pricingOptions: 'pricePerPerson' } ],
capacity: 0,
date: { year: 2019, month: 7, day: 1 },
time: { hour: 9, minute: 0 },
pickupTime: null }
but another person using a java client tells me that he sees:
2019-06-26 10:59:39,442 ← getProductAvailability::grpc response {date { year: 2019 month: 7 day: 1 } time { hour: 9 } rates { rateId: "1" pricePerPerson { pricingCategoryWithPrice { pricingCategoryId: "30" price { currency: "EUR" amount: "145" } } pricingCategoryWithPrice { pricingCategoryId: "31" price { currency: "EUR" amount: "150" } } } }}
where capacity is not set.
If it's value is 1 and not 0, everything works well everywhere.
Is it possible?
How can i force the server to output the value?
I already tried using capacity = parseInt(capacity)

Mongoose aggregating the documents, based on the time difference

I have a schema with following fields
var MySchema = new Schema({
roomId: { type: String, required: true, ref: 'Room' },
timestamp: Date,
currentTemp: Number,
outsideTemp: Number,
desiredTemp: Number
});
Now I want to aggregate the documents in this collection and return the response like
{
timestamp: [1,2,3,4,5,6,7,8,9,10..,24],
currentTemp: [34,45,.....],
outsideTemp: [14,45,.....],
desiredTemp: [34,45,.....]
}
I have some match condition and I am able to select the documents based on the match condition. Now I want to aggregate the selected documents and produce a result like above.
The timestamp value is bound by a lower bound and upper bound. The upper bound and lower bound is passed from the client and the aggregation should be based on the difference between the lower and upper. If the difference is
- 1 day : group by hour
- 3 days : group by 6 hrs
- 7 days : group by 12 hours
- < 30 days : group by day
- > 30 days : group by week
currentTemp is the average of all the documents currentTemp property in that time period. Similarly for outsideTemp and desiredTemp.
How can I do this ?
EDIT:
Sample data set
{
"_id": "5656bfd94d6f6304ff140000",
"roomId": "5656bc124d6f6335051b0000",
"timestamp": "2015-11-15T08:59:20Z",
"currentTemp": 10.55,
"outsideTemp": 43.83,
"desiredTemp": 21.32
}, {
"_id": "5656bfd94d6f6304ff150000",
"roomId": "5656bc124d6f633505200000",
"timestamp": "2015-06-01T06:33:49Z",
"currentTemp": 32.47,
"outsideTemp": 49.65,
"desiredTemp": 20.99
}, {
"_id": "5656bfd94d6f6304ff160000",
"roomId": "5656bc124d6f633505250000",
"timestamp": "2014-12-31T23:47:54Z",
"currentTemp": 35.69,
"outsideTemp": 29.91,
"desiredTemp": 20.15
}, {
"_id": "5656bfd94d6f6304ff170000",
"roomId": "5656bc124d6f6335051e0000",
"year": 2015,
"month": 3,
"day": 12,
"hour": 21,
"minute": 13,
"second": 38,
"inDST": true,
"timestamp": "2015-11-14T07:56:42Z",
"currentTemp": 27.65,
"outsideTemp": 41.4,
"desiredTemp": 24.68
}

Resources