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

168
Visualizações
Wait for a query in MongoDB

I have this async method written in Typescript to query, with the nodejs driver, a MongoDB; the compiler indicates that the "await" before "myConnectedClient" has no effect on the type of this expression; I'm confused: is the call to the aggregate() asynchronous? So, I have to wait, or not?
Thanks.

async findQuery<T>(
    collection: string,
    findParams: Query<T>,
    sort: Sort<T>,
    myConnectedClient: MongoClient
  ) {
    const firstResult = await myConnectedClient // the compiler indicates await is useless
      .db("ZZZ_TEST_ALL")
      .collection("my_collection_01")
      .aggregate<string>([{ $project: { _id: 0, name: 1 } }]);
    firstResult.forEach((field) => {
      console.log(`Field: ${field}`);
    });
  }

UPDATE: I have to add .toArray() after the .aggregate() call; but why? Can anybody explain me the mechanism? aggregate() has not callback and does not return a promise? Are there alternatives to .toArray()? Thanks.

// now await it's ok
const firstResult = await myConnectedClient
      .db("ZZZ_TEST_ALL")
      .collection("my_collection_01")
      .aggregate<string>([{ $project: { _id: 0, name: 1 } }]).toArray();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Aggregate is sync and returns an AggregationCursor.

The cursor has number of async methods to retrieve actual data: toArray, forEach, or simple iterator

In the first snippet firstResult is the cursor, so there is no need to await. You use firstResult.forEach to log the results. It does return the promise but you ignore it, which will bite you - the promise returned by findQuery will be resolved immediately while forEach will iterate results in parallel. To keep the promise chain you should do

const firstResult = myConnectedClient.......; 
return firstResult.forEach(......);

so the promise returned from the findQuery will be resolved only when forEach is resolved, e.g. you finish iterate the results.

In the second "update" snippet, the firstResult is the data, so you need the await to get it from toArray(). The equivalent with explicit cursor would be:

const cursor = myConnectedClient.......; 
const firstResult = await cursor.toArray();
about 4 years ago · Juan Pablo Isaza Relatório
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