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

333
Vistas
Speed ​issue and also memory error when querying in Mongo

I have a table that contains over 100,000 records. Server: node.js/express.js. DB: mongo On the client, a table with a pager is implemented. 10 records are requested each time.

When there were 10,000 records, of course, everything worked faster, but now there was a problem with speed and not only.

My aggregation:

import { concat } from 'lodash';
...
let query = [{$match: {}}];
query = concat(query, [{$sort : {createdAt: -1}}]);
query = concat(query, [
    {$skip : (pageNum - 1) * perPage}, // 0
    {$limit : perPage}                 // 10
]);
return User.aggregate(query)
           .collation({locale: 'en', strength: 2})
           .then((users) => ...;

2 cases:

  1. first fetch very slow

  2. when I click to last page I got error:

    MongoError: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.

Please, tell me, I am building the aggregation incorrectly, or is there a problem with memory on the server as the error says and additional nginx settings are needed (another person is engaged in this) or is the problem complex, or perhaps something else altogether?

Added:

I noticed that the index is not used when sorting, although it should be used?

enter image description here

aggregation to execute console.log =>

[
 {
      "$match": {}
 },
 {
      "$lookup": {
           ...
      }
 },
 {
      "$project": {
           ...,
           createdAt: 1,
           ...
      }
 },
 {
      "$match": {}
 },
 {
      "$sort": {
           "createdAt": -1
      }
 },
 {
      "$skip": 0
 },
 {
      "$limit": 10
 }
]

Thanks for any answers and sorry my English :)

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It does say that you've memory limit, which makes sense, considering that you're trying to filter through 100,000 requests. I'd try using return User.aggregate(query, { allowDiskUse: true }) //etc, and see if that helps your issue.

Whilst this isn't the documentation on the Node.js driver specifically, this link summaries what the allowDiskUse option does (or in short, it allows MongoDB to go past the 100MB memory limit, and uses your system storage to temporarily store some information while it performs the query).

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