Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda