mongodb aggregate documents by dates nearest to specific hour/minute in day - node.js

I have documents in my mongodb, this documents have event field - this fild type is date. The year, month , day, does not matter, means only the time during day. I want the cron script,every day, to aggregate from mongodb the documents with the event (date typed) field to be in nearest 10 minutes (to the script calling date). How to implement it in right way?

db.mytable.find(
{
"event": {
$gt: new Date(new Date().getTime() - (10 * 60 * 1000))
}
})
This query will find all documents that have an "event" property with a value within the past 10 minutes. new Date() without arguments returns a Date representing "right now". We pull the numeric epoch time in milliseconds from that and subtract 10 minutes. More specifically, we subtract (10 minutes * 60 seconds per minute * 1000 milliseconds per second), so that we convert to the correct units. We then use that value to construct another new Date(...), and this is the one that goes into the $gt (greater-than) filtering condition.
You mentioned a need for "aggregation". If so, then this same query can also be used within any Aggregation Pipeline that you need.

Related

$sum function to do sum of hours and min

I have one user who has multiple tasks with hours and minutes. which I stored in milliseconds
user's tasks schema
tasks:{
userId: String //getting from the session(storing the task schema)
time:{type: "number"}
}
User's input like this
time:{
hrs: 10,
min: 45
}
I converted the user input in milliseconds using the date.getTime() function by setting the hours and minutes.
Which will later be stored as milliseconds in the schema.
The output will be ( suppose there are multiple tasks) 10 + 10 (the final output should be
20hrs
So I Want to do the sum of all the multiple hours based on a specific user(userId) stored in
tasks
Should I Use the Aggregation method for this problem?

bin() adx scalar function returns an aggregate every round hour but I want it to be on the half hour

| take 2000000
| summarize Value = avg(Value) by bin(TimeStampOfValue, 1h)
I have an adx table with Value and a Timestamp and I run this query I get the avg Value every hour for example:
TimeStampOfValue
Value
2022-01-30T22:00:00
500
2022-01-30T23:00:00
499,99
I'd like it to return:
TimeStampOfValue
Value
2022-01-30T22:30:00
500
2022-01-30T23:30:00
499,99
How do I shift the 'by bin' by 30 minutes? so it runs hourly on the half hour mark? Is this even possible?
One solution is to use 'bin_at' with a specific time so it starts hourly from there, is this the only way?

How do I query the dates in mongodb such that the entries with less than 1 hour difference is removed?

MongoDB collection
I have this collection. I want to query entries so that either one of key "2" or "3" would be removed because the createdAt entries have a difference less than 1 hour. What would be the query?
Query
uses $dateTrunc with unit hour, that means keep the date as it is, but make zero all the left after the date
group by that trunc date => dates with same hour are in the same group
keep only one of them
*query doesn't work with difference, it works by same hour, for example 1:01 is the same with 1:59 but 1:59 is not same group with 2:01
*difference is complicated i think, because 2 dates can have difference 1 hour, and be in different days, months, or even years,
also we have to decide which ones to keep, in case of chains of dates.
Test code here
aggregate(
[{"$group":
{"_id":{"$dateTrunc":{"date":"$createdAt", "unit":"hour"}},
"doc":{"$first":"$$ROOT"}}},
{"$replaceRoot":{"newRoot":"$doc"}}])
You can use $gt query. See the document here
And here is a code example. Just change the places with your property, which is createdAt in your scenario.
let user = await User.deleteMany({
createdAt: { $gt: Date.now() - 3600000 } // 3600000 means 1 hour in millisecond
});
// Rest of your code

how to convert milliseconds into HH:MM:SS format in mongodb(3.4 version)

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)

Dynamodb TTL 24hours

Hi I am wonder about how to set the time to live in dynamo db.
I understand that I have to create a field, could be called ttl and set the value to be deleted, but in which format innodejs do I have to save to use for the ttl field for 20 or 24 hours?
Thanks for help.
From the official DynamoDB documentation:
TTL compares the current time in epoch time format to the time stored
in the Time To Live attribute of an item. [...]
Note
The epoch time
format is the number of seconds elapsed since 12:00:00 AM January 1st,
1970 UTC.
In Javascript, you can get the number of milliseconds since the epoch by doing Date.now(). Once you have that, you can divide everything by 1000 to get the seconds (also rounding to the nearest integer value) and finally add the number of seconds in the TTL that you want.
This means that if you want to set the expiration time 24 from now, you can easily set the TTL field with the value expirationTime calculated this way:
const SECONDS_IN_AN_HOUR = 60 * 60;
const secondsSinceEpoch = Math.round(Date.now() / 1000);
const expirationTime = secondsSinceEpoch + 24 * SECONDS_IN_AN_HOUR;

Resources