Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

363
Views
Sequelize: How to select subset of an array column?

I have a table with an array column called tokens

I can query it via npm sequelize with no issues, Sometimes this column may have upto 20k elements in the array which i dont always need. I need just 10 elements from it

In SQL this would be

select tokens[:10] from schema.table 

How I do this using sequelize ?

This is what I'm doing now

const whereClause = {
        where: { active: true },
        attributes: {
          exclude: ['tokens'],
          include: ['tokens[:10]'],
        },
      };

table.findAll(whereClause);

This gives the following error

original: error: column "tokens[:10]" does not exist

It is looking for a column named "tokens[:10]" instead of taking a subset. What am I doing wrong ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use literals when you select attributes in sequelize. You can try something like this,

    const whereClause = {
        where: { active: true },
        attributes: [
           [Sequelize.literal(`tokens[:${sub-array-length}]`), 'tokens']
        ]
      };

    table.findAll(whereClause);

Note the use of colons to denote the sub-array slicing index

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!