limit on records for every date sequelize nodejs - node.js

I have a model which has large number of records, I want records of current month which is working fine, but it's a large data set so i want query like,
Records of October with limit of lets say 30 for every day, like there should be result per day but for every day there should be maximum 30 rows. if i put limit generally it limit to the whole data i want limit on date basis.
Here is my model query I am using sequelize
MyModel.findAll({
where: {
created_at: {
[Op.gt]: search_from,
[Op.lte]: search_to
}
},
order: [['created_at', 'DESC']],
limit: 30
})
Here is want limit that work for every day instead if on whole records.

Related

How to update multiple rows at once in the database with sequelize

I have table "BusinessHours" that contains days of the week of the store with opening Hours and Closing hours.
I want to update the values with one query! how to achieve this with sequelize ?
That's the table of the business hours and these are related to one store
Note: that in one query I want to update the whole row for each day.
In Sequelize, assuming you have a model setup for this, you can update all of the applicable days of the week that have the same hours by using the following:
await BusinessHours.update(
{
openingTime: <date>,
closingTime: <date>,
},
{
where: {
dayOfTheWeek: ['mon', 'tues', ...],
}
}
)
If you would like to update any series of days that have different values, those would have to be separate queries.
Keep in mind, you may want to include the storeId in your where statement depending on your requirements.
https://sequelize.org/api/v6/class/src/model.js~model#static-method-update

Count records for each day between the startdate and enddate in sequelize ORM (Performing this query in nodejs)

I am passing start_time and end_time as a query parameter in an API and want to have count of the records on each day from start_time and end_time.
It is giving me total count of records between the specified start and end_time but I want count for each day individually.
Example start_time: 2017-10-07 and end_time is 2017-10-10
So, I want count for the number of records on the date: 2017-10-07, 2017-10-08, 2017-10-09 and 2017-10-10.
You want a GROUP BY clause in your sql to group your count by days. Here's documentation on how to do that with Sequelize.
https://sequelize.org/master/manual/model-querying-basics.html#grouping
And if your date isn't just a single day, but has hours and minutes, you might need to do some date truncation in your group by. Here's some info on that:
Sequelize grouping by date, disregarding hours/minutes/seconds

How do I group datetimes with a sqlite windowing function?

Let's say I have a table with the following fields:
customerid, transactiontime, transactiontype
I want to group a customer's transactions by time, and select the customerid and the count of those transactions. But rather than simply grouping all transaction times into certain increments (15 min, 30 min, etc.), for which I've seen various solutions here, I'd like to group a set a customer's transactions based on how soon each transaction occurs after the previous.
In other words, if any transaction occurs more than 15 minutes after a previous transaction, I'd like it to be grouped separately.
I expect the customer to generate a few transactions close together, and potentially generate a few more later in the day. So if those two sets of transactions occur more than 15, 30 minutes apart, they'll be grouped into separate windows. Is this possible?
Yes, you can do this using a window function in SQLite. This syntax is a bit new to me, but this is how it would start:
select customer_id,
event_start_minute,
sum(subgroup_start) over (order by customer_id, event_start_minute) as subgroup
from (
select customer_id,
event_start_minute,
case
when event_start_minute - lag(event_start_minute) over win > 15
then 1
else 0
end as subgroup_start
from t1
window win as (
partition by b
order by c
)
) as groups
order by customer_id, event_start_minute

Is there a way to query for documents that have a certain millisecond in a timestamp? [Mongo]

I have a collection on my db with a "created" field that is a timestamp.
What I need to do is somehow get the milliseconds on that timestamp, divide it by 10 and then floor it and THEN match it to a number.
What I'm trying to achieve with this is have a random spread of this documents into "batches" that range from 0-100.
Is there a way to do all this in one query? Basically something like:
Tried aggregation to add that field but then I'm getting all the documents and filtering, was wondering if there's a way to do all this "in-query"
collection.find({<created.convertToMilliseconds.divideByTen>: X }})
// X being any number from 0 to 100
I

Sort and limit capping results

I got a collection containing 20,000+ documents where only id is indexed.
I'm want to query using limit and a sort, but the limit kicks in before the sort.
collection.find({})
.sort({kick_return_yards: -1}) // desc largest to smallest
.limit(500)
.toArray (err, records){
});
kick_return_yards is a positive whole number. I know I have values for this number ranging from 0 to 177.
With above query I only get documents where kick_return_yards is 0. So it seems sort is executed after limit.
How do I get around this hoop?

Resources