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
Related
I'm using 'bulkCreate' with 'updateOnDuplicate' option to achieve bulkUpdate, like this:
Product.bulkCreate(to_update, {
updateOnDuplicate: ['name', 'description'],
});
It works fine, but I can't find a way to get the affected rows count.
I tried adding 'updatedAt' to the fields to update and then counting the rows where 'updatedAt' is greater than the time before starting the execution. But ended up with all rows receiving CURRENT_TIMESTAMP as 'updatedAt'.
I'm sure this is a standard requirement, but then again, so is the 'bulkUpdate'.
Any ideas?
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.
Description:
I have a table ProductGroupItems that is in a 1 to 1 relationship with BonusGroupItems and ThresholdGroupItems. Sometimes a ProductGroupItem is a bonus item other times its a threshold item. I chose this so that ProductGroupItems wouldn't have any columns that were null.
Question:
How do I insert multiple ProductGroupItems and its corresponding 1 to 1 table (Threshold or Bonus) using Sequelize.bulkCreate?
What I have tried:
Looping through values and making a separate create() each time. This does work but fires a query for EACH record, obviously.
models.ProductGroupItems.bulkCreate(productItems, { return: true }) then doing something fancy with the values that come back. This was too confusing and I ended up not being able to figure it out.
Any help?
I have a requirement to filter some records on the basis of period for example get me all products count between 201704 and 201705.
So my emit is something like:
emit([doc.productId, doc.period],1)
and reduce used is _count. But when i am trying to filter on startkey and endkey it gives me all records. I am trying something like:
startkey=["201604"]&endkey=[{},"201605"]
where my key structure is something like key:
[
"aws-60826348802",
201703
]
I dont want to reverse the order of elements in key as i want a count on ids by defining the group_level=1. Could anyone help me out. I did not get any solution yet.
In order to be able to select (with startkey/endkey), your index needs to be in in that order. Let's say your docs look like this:
{
"_id": "someid",
"product": "aws-60826348802",
"date": "201604",
"price": 42.42
}
You could create a map function to index the documents in date order like so:
function(doc) {
emit([doc.date, doc.product], doc.price);
}
You now have a two-dimensional key (on date & product code) and the value is emitted is the price.
This allows you to select items by date:
?startkey=["201604"] // find items where the date is >= 201604
?startkey=["201604"]&endkey=["201605"] // find items in April 2016
if you choose a reducer (_count, _sum, or _stats) you can also perform aggregation:
?startkey=["201601"]&endkey=["201701"]&group_level=1 // find items in 2016, group by month
?startkey=["201601"]&endkey=["201701"]&group_level=2 // find items in 2016, grouped by month and product code
This could get you monthly totals of product sales, for example.
I have a date of joining field as doj (type : Date) in my MongoDB collection. I want to find the records of all employees who joined more than a month before from today. However it is giving any record where the doj is just even one day greater than the one month old date from today. I am not getting the one month older doj from today. Please help me with the mistakes that I am making.
I am using the following code :
var dt = new Date();
dt.setMonth(dt.getMonth() - 1);
collection.find({doj: {$gt:dt}}, function(error, docs) {
docs.forEach(function (doc) {
console.log("DOJ = "+ doc.doj);
}
});
If you want records with date of joining older then one month ago, you should query for {doj: {$lt:dt}}