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

619
Vistas
Error: Collection method aggregate is synchronous

I'm trying the following code:

const Conn = mongoose.createConnection('mongodb://127.0.0.1:27017/db');

const addresses = Conn.collection('users').aggregate([
  {
    $project: {
      _id: false,
      ethAddr: true,
    }
  }
]);

I receive the following error:

[...]\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:100
        throw new Error('Collection method ' + i + ' is synchronous');
              ^

Error: Collection method aggregate is synchronous
    at NativeCollection.<computed> [as aggregate] ([...]\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:100:15)
    at file:///[...]/backend/scripts/dbGetMerkleRoot.js:11:46
    at file:///[...]/backend/scripts/dbGetMerkleRoot.js:28:3
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

What in cattle's name I'm doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It seems to be a problem with the way mongoose connects to the database. Creating the connection but not connecting prior to invoking the aggregation method causes that exception to be thrown. I should've used it this way:

// create custom connection
const Conn = mongoose.createConnection();

// connect to database
await Conn.openUri('mongodb://127.0.0.1:27017/db');

// @type {AggregationCursor}
const addresses = Conn.collection('users').aggregate([
  {
    $project: {
      _id: false,
      ethAddr: true,
    }
  }
]);

console.log( await addresses.toArray() );

It's frustrating the exception itself is poorly documented.

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