How can I use some matching conditions and logical operations(lt, et, gte etc.) on Jsonb array of objects ([{...}, {...}]) in PostgreSQL using Sequelize ORM.
TableName: calls
| id | direction | metaData |
|---|---|---|
| 1 | inbound | [{...}, {...}] |
| 2 | outbound | [{...}, {...}] |
metaData:
[{
"id": 1,
"audioUrl": "https://xyz.wav",
"duration": 136,
"agentName": "Ext 204",
"calledNumber": "123456789",
"callingNumber": "987654321",
"startedAt": "2020-08-31 5:07:00",
"endedAt": "2020-08-31 11:07:20",
},
{
"id": 2,
"audioUrl": "https://abc.wav",
"duration": 140,
"agentName": "Ext 210",
"calledNumber": "123456789",
"callingNumber": "987654321",
"startedAt": "2020-08-31 10:07:00",
"endedAt": "2020-08-31 10:09:20",
}]
I want to search for data from the table base on the metaData conditions using Sequelize ORM.
Example 1: fetch all rows where agentName='Ext 204' AND duration >= 136
Example 2: fetch all rows where agentName='Ext 204' AND startedAt >= '2020-08-31 10:07:00'
My model query:
const Op = Sequelize.Op;
const resp = await callModel.findAll({
attributes: ['id', 'direction'], // table columns
where: {
metaData: { // jsonB column
[Op.contains]: [
{agentName: 'Ext 204'},
],
},
},
});
The above model search call executes following query:
SELECT "id", "direction" FROM "calls" AS "calls" WHERE "calls"."metaData" @> '[{"agentName":"Ext 205"}]';
My Attempt: which is not working
callModel.findAll({
attributes: ['id', 'direction'], // table columns
where: {
metaData: { // metaData
[Op.and]: [
{
[Op.contains]: [
{agentName: 'Ext 204'},
],
},
{
duration: {
[Op.lt]: 140
}
}
]
},
},
});
Resultant query:
SELECT "id", "direction" FROM "calls" AS "calls" WHERE ("calls"."metaData" @> '[{"agentName":"Ext 205"}]' AND CAST(("calls"."metaData"#>>'{duration}') AS DOUBLE PRECISION) < 140);
Required: Unable to add some more conditions as duration < 140
metaDatamatch the conditions" or "fetch rows where all objects inmetaDatamatches"?metaData,duration > 136only matches formetaData.id=2and not formetaData.id=1, in this case do you want this record (id=2, direction=outbound) to return?