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

259
Vistas
GraphQL MongoDB: Implementing cursor-based $text $search pagination

In a normal cursor pagination, I would filter and sort the documents based on created field, which is pretty simple. like this:

// `cursor` is a string
const books = await this.BooksModel.find({
    ...(cursor ? { created: { $lt: new Date(cursor) } } : null),
})
    .sort({ created: -1 })
    .limit(first)

Meanwhile when implementing search, I (think I) have to sort and filter the documents based on textScore metadata, which could have a duplicates. To prevent this I need another field to filter, which is _id. I imagine it would be something like this:

// `cursor` is { id: string, score: number }
const books = await this.BooksModel.aggregate(
    [
        { $match: { _id: { $ne: ObjectId(cursor.id) }, $text: { $search: 'sed' } } },
        { $project: { title: 1, score: { $meta: 'textScore' } } },
        { $match: { score: { $lt: cursor.score } } }
    ]
)

To achieve the example above, I need the cursor to contain not only the document _id (like a normal pagination) but also the score. I imagine the length of the cursor (decoded) would be very long compared to the normal one. Moreover, the cursor now contains an object instead of just string, which is pretty weird to a noob like me. Is this really the way to build cursor-based search pagination?

over 4 years ago · Santiago Trujillo
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