Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

366
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda