Need to count a joined table rows sequelize - javascript

I am using sequelize to query on a sqlserver database. I have two tables:
data: columns - id, name, team, type
history:columns - id, node, date, status, data_id(foreignkey)
and a relation
history.belongsTo(data, {foreignKey: 'data_id'}
data.hasMany(history, {foreignKey: 'data_id'})
My query is:
dataModel.findAll({
attributes: ['name'],
include: [{
model:historyModel
}]
})
My result looks like this:
[
{
name: "1",
history: [
{
...
}
]
},
{
name: "2",
history: [
{
...
}
]
}
]`
I want that instead of the history array I will have the count of history objects in each one. The query in sql is:
select data.name, count(history.data_id) count
from history
inner join data on data.id=history.data_id
group by history.data_id, data.name

You can do it this way:
dataModel.findAll({
attributes: {
include: [[Sequelize.fn("COUNT", Sequelize.col("history.data_id")), "historyModelCount"]]
},
include: [{
model: historyModel, attributes: []
}],
group: ['data.id']
});

Related

Sequelize - MySQL error: SELECT list is not in GROUP BY clause

I have this function which returns the categories containing the product with the lowest price.
async getAllWithMinPrice(req, res, next) {
try {
const categories = await Category.findAll({
attributes: ["id", "name"],
group: ["products.id"],
include: [
{
model: Product,
attributes: [
"id",
"name",
"pluralName",
"description",
"image",
[Sequelize.fn("MIN", Sequelize.col("price")), "minPrice"],
],
include: [
{
model: Type,
attributes: [],
},
],
},
],
});
return res.json(categories);
} catch (e) {
return next(ApiError.badRequest(e.message ? e.message : e));
}
}
But I am getting an error: "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'fotoluks.category.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by".
On my test server, I just disabled ONLY_FULL_GROUP_BY in MySQL, but I don't have permission to do so on the current server.
As far as I understand, to solve this problem, you need to use any_value, but I just can't figure out how to do it in Sequelize.
P.S. In a similar function, I had exactly the same error, then I solved it by simply adding attributes: [], but in this case I can't do it.
you need to include those attributes in group by:
where: { //condition },
attributes: [
"provider_id",
"product_id",
"quantity",
[Sequelize.fn("sum", Sequelize.col("product_price")),"total_amount"],
],
group: ["provider_id", "product_id", "quantity"],

Sequelize query with nested array object json

I have data stored in a Postgres 11 table, one of the columns has a json object as it follows:
id
data
client_id
1
JSON
BR_123
{
"shed": false,
"transactions": [
{
"id": 1,
"value": 5000,
"depositDate": "2021-09-20T10:29:05.000Z",
"expirationDate": "2022-03-29T10:29:05.000Z",
"requestDate": "2021-09-17T17:04:43.000Z"
}
],
"applies": false,
"importValue": null,
"depositValue": null
}
What I'm trying to do is select all of the entries that have a 'transactions.value' greater or equal than a given amount.
Sequelize:
const clients = await ComercialImports.findAll({
where: {
status: 'Running'
},
attributes: ['id', 'mode', 'importerId'],
include: [
{
model: ComercialImportsTransactions,
as: 'importTransaction',
attributes: ['id', 'data', 'clientId'],
required: true,
where: {
data: {
transactions: {
value: {
[Op.gte]: 5000
}
}
}
}
}
]
});
Unfortunately, for some reason this query won't work, as it always return 0 results. I can't seem to access any values stored on the transactions entry. All other entries are accessible (e.g. I can query for all data with 'shed' = true)
What am I doing wrong?

Edit multiple objects in array using mongoose (MongoDB)

So I tried several ways, but I can't, I can modify several objects with the same key but I can't modify any with different keys, if anyone can help me is quite a complex problem
{
id: 123,
"infos": [
{ name: 'Joe', value: 'Disabled', id: 0 },
{ name: 'Adam', value: 'Enabled', id: 0 }
]
};
In my database I have a collection with an array and several objects inside which gives this.
I want to modify these objects, filter by their name and modify the value.
To give you a better example, my site returns me an object with the new data, and I want to modify the database object with the new object, without clearing the array, the name key never changes.
const object = [
{ name: 'Joe', value: 'Hey', id: 1 },
{ name: 'Adam', value: 'None', id: 1 }
];
for(const obj in object) {
Schema.findOneAndUpdate({ id: 123 }, {
$set: {
[`infos.${obj}.value`]: "Test"
}
})
}
This code works but it is not optimized, it makes several requests, I would like to do everything in one request, and also it doesn't update the id, only the value.
If anyone can help me that would be great, I've looked everywhere and can't find anything
My schema structure
new Schema({
id: { "type": String, "required": true, "unique": true },
infos: []
})
I use the $addToSet method to insert objects into the infos array
Try This :
db.collection.update({
id: 123,
},
{
$set: {
"infos.$[x].value": "Value",
"infos.$[x].name": "User"
}
},
{
arrayFilters: [
{
"x.id": {
$in: [
1
]
}
},
],
multi: true
})
The all positional $[] operator acts as a placeholder for all elements in the array field.
In $in you can use dynamic array of id.
Ex :
const ids = [1,2,..n]
db.collection.update(
//Same code as it is...
{
arrayFilters: [
{
"x.id": {
$in: ids
}
},
],
multi: true
})
MongoPlayGround Link : https://mongoplayground.net/p/Tuz831lkPqk
Maybe you look for something like this:
db.collection.update({},
{
$set: {
"infos.$[x].value": "test1",
"infos.$[x].id": 10,
"infos.$[y].value": "test2",
"infos.$[y].id": 20
}
},
{
arrayFilters: [
{
"x.name": "Adam"
},
{
"y.name": "Joe"
}
],
multi: true
})
Explained:
You define arrayFilters for all names in objects you have and update the values & id in all documents ...
playground

MongoDB: Project only the items that was queried for in the array?

I have a user document, each user has an array of objects
Given an array of item tags, I need to find the user whose item array has the item-tag, and return the entire user object except the items array, in which I only want to return the first item tags that existed in the tagArray that was used for the intial query.
//user document
{
user: 'John',
items: [ObjectId('ABC'), ObjectId('123') ...]
}
//item document
{
_id: ObjectId('ABC'),
tag: 'some-unique-id'
},
{
_id: ObjectId('DEF'),
tag: 'some-unique-tag'
}
Users have a 1-to-N relationship with items, the items may repeat within the User's items array.
This is what I current have, which returns the entire user object, but also all the items within the array.
const tagArray = [ 'some-unique-id', 'some-unique-tag']
items.aggregate([
{ $match: { 'tag': { $in: tagArray } }},
{ $lookup: {
from: "users",
localField: "tag",
foreignField: '_id',
as: 'userInfo'
}
},
{
$project: {??} //<--- I'm pretty sure I'm missing something in the project
])
Outcome that I have now:
{
_id: ObjectId('ABC'),
tag: 'some-unique-id'
userInfo : [ {user: 'John', items: [ObjectId('ABC'), ObjectId('123') ...] }]
}
What I want to achieve:
{
_id: ObjectId('ABC'),
tag: 'some-unique-id'
userInfo : [ {user: 'John', items: [ObjectId('ABC')]} ]
}
Edit:
There is a similar question here : Retrieve only the queried element in an object array in MongoDB collection
However in my case, I need the filter condition to be "one of the the tags that is in the tagArray.
Any suggestion or pointers would be appreciated, thank you!
I don't know if I understood well what you need, but I think this is a good start (maybe you can modify it by yourself):
Test data:
// users collection
[
{
user: "John",
items: [
ObjectId("5a934e000102030405000002"),
ObjectId("5a934e000102030405000003")
]
}
]
// items collection
[
{
_id: ObjectId("5a934e000102030405000002"),
tag: "some-unique-id"
},
{
_id: ObjectId("5a934e000102030405000009"),
tag: "some-unique-tag"
}
]
}
Query:
db.users.aggregate([
{
$lookup: {
from: "items",
localField: "items",
foreignField: "_id",
as: "userInfo"
}
},
// create new fields inside the userInfo array
{
$project: {
"userInfo.user": "$user",
"userInfo.items": "$items",
"tag": {
$arrayElemAt: ["$userInfo.tag", 0]
}
}
},
// filter the userInfo.items field, based on _id field
// it's important to use $arrayElemAt here
{
$addFields: {
"userInfo.items": {
$filter: {
input: {
$arrayElemAt: [
"$userInfo.items",
0
]
},
as: "i",
cond: {
$in: [
"$$i",
[
"$_id"
]
]
}
}
}
}
}
])
Result:
[
{
"_id": ObjectId("5a934e000102030405000002"),
"tag": "some-unique-id",
"userInfo": [
{
"items": [
ObjectId("5a934e000102030405000002")
],
"user": "John"
}
]
}
]

Combined WHERE clause for all INCLUDEs

i want to perform this query in sequelize models:
SELECT Cou.country_id,cou.country_name, Sta.state_id, Sta.state_name, Dis.district_id,
Dis.district_name,Cit.city_id, Cit.city_name,Loc.location_id,Loc.location_name,Sub_Loc.sub_location_id,Sub_Loc.sub_location_name,Prop.property_id,Prop.property_name,Prop.hp_builders_id,
Bud.builders_id,Bud.builders_name
FROM hp_country Cou
INNER JOIN hp_state Sta ON Cou.country_id = Sta.hp_country_id
INNER JOIN hp_district Dis ON Sta.state_id = Dis.hp_state_id
INNER JOIN hp_city Cit ON Dis.district_id = Cit.hp_district_id
INNER JOIN hp_location Loc ON Cit.city_id = Loc.hp_city_id
INNER JOIN hp_sub_location Sub_Loc ON Loc.location_id = Sub_Loc.hp_location_id
INNER JOIN hp_property Prop ON Sub_Loc.sub_location_id=Prop.hp_sub_location_id
LEFT JOIN hp_builders Bud ON Prop.hp_builders_id=Bud.builders_id
where (Cou.country_status=1 AND Sta.state_status=1 AND Cou.country_id=1)
AND (Dis.district_name LIKE '%ky%' OR Cit.city_name LIKE '%ky%' OR Loc.location_name LIKE '%ky%' OR Sub_Loc.sub_location_name LIKE '%ky%' OR Prop.property_name LIKE '%ky%')
I tried to write in this manner but i did not achieved my target query :
Included more nested models because of all are inner joins and i tried with required is true in include box.how to do [OR] condition for columns which is in different tables
hp_country.findAll({
attributes: ['country_id', 'country_name'],
where: {
country_status: 1,
country_id: 1
},
include: [{
model: hp_state,
attributes: ['state_id', 'state_name'],
where: {
state_status: 1,
},
include:[{
model: hp_districts,
attributes: ['district_id', 'district_name'],
where: {
district_status: 1
},
include:[{
model: hp_city,
attributes: ['city_id', 'city_name'],
where: {
city_status: 1,
city_name :{
$like: '%ma%'
}
},
include:[{
model: hp_location,
attributes: ['location_id', 'location_name'],
where: {
location_status: 1,
location_name :{
$like: '%ma%'
}
},
include:[{
model: hp_sub_location,
attributes: ['sub_location_id', 'sub_location_name'],
where: {
sub_location_status: 1,
sub_location_name :{
$like: '%ma%'
}
}
}]
}]
}]
}]
}]
})
I tried to fix your query, but didn't test it. If you face any problem just notify me:
hp_country.findAll({
attributes: ['country_id', 'country_name'],
where: {
//main AND condition
$and: [
//first joint condition
{
$and: [
{ country_status: 1 },
{ country_id: 1 },
Sequelize.literal("Sta.state_status = 1") //I put 'state_status' here because it was a joint condition to be true
],
},
//second joint condition
{
$or: [
Sequelize.literal("Dis.district_name LIKE '%ky%'"),
Sequelize.literal("Cit.city_name LIKE '%ky%'"),
Sequelize.literal("Loc.location_name LIKE '%ky%' "),
Sequelize.literal("Sub_Loc.sub_location_name LIKE '%ky%'"),
Sequelize.literal("Prop.property_name LIKE '%ky%'")
]
}
]
},
include: [
{
model: hp_state,
attributes: ['state_id', 'state_name']
},
{
model: hp_districts,
attributes: ['district_id', 'district_name']
},
{
model: hp_city,
attributes: ['city_id', 'city_name']
},
{
model: hp_location,
attributes: ['location_id', 'location_name']
},
{
model: hp_sub_location,
attributes: ['sub_location_id', 'sub_location_name']
}
]
})

Categories

Resources