简体   繁体   中英

translate the postgres ANY() operator into Node js using Massive library

I am trying to update an existing postgres query in the Nodejs code base.

Below is the existing query to select based on name

constructor(schema){
 this.table = schema.employee_tb;
}

row = await this.table.findOne({ name });

Below is the Postgres query I am trying to translate and update the above code into...

select * from table where name = 'john' and '3467' = ANY(group_number)

I tried

row = await this.table.findOne({ name : 'john' && ANY(group_number): '3467' });

For which it is throwing an error.

Basically, I am trying to have the ANY operator in using the massive library.

NOTE: group_number is character_varying[] which has data like {3467, 3455, 3421}..... the query should return the row which contains the group number in the group_number column.

Please help, Thanks in advance.

If you are looking to findOne that has the name 'john' AND the group_number '3467', you need to update your object syntax.

row = await this.table.findOne({ name : 'john', group_number : '3467' });

If you are using Sequelize here for the findOne method, I would take a look at the docs here where you can add in the ANY operator with Op.any in your query, but you will need to have that character_varying[] ready to pass into the function.

This worked!!

row = await this.table.where(`name = 'jhon' and '3467' = ANY(group_number)`);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM