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

195
Visualizações
What is a Mongoose Query object ? Is it a promise?

Mongoose models provide several static helper functions for CRUD operations. Each of these functions returns a mongoose Query object.

For example :

    let query = Model.find();

This returns a <Query> Object which is chainable.

    async function() => {
        let newQuery = query.where("key").equals("value");
        let result = await newQuery;
    }

Q1• What exactly is a Query Object returned from Model.find()?

Q2• How does chaining work?

Are <Query> objects like promise since I am using asynchronous functions?

Kindly link any kind of docs that might clear my issue ? I am relatively new to this so if my question makes no sense please correct me :-(

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

For questions like these, it is best to consult the TypeScript definitions for the module. query.d.ts in particular holds the answers.

Q1• What exactly is a Query Object returned from Model.find()?

find's signature looks as follows:

find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
      callback?: Callback<ResultDoc[]> | undefined
    ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;

So the return type is QueryWithHelpers with some generics, which resolves to Query with some generics. Now to look at it's definition it is quite long. It is a regular object with the following implementations:

  • interfaces to call exec(), find(), findOne, etc. These are your chaining methods (Q2).
  • It implements an async iterator: [Symbol.asyncIterator](): AsyncIterableIterator<DocType>; which lets you use things like for (let .. of ..) on it
  • It implements an interface to work like a promise: then: Promise<ResultType>['then'];. This lets you do find(...).then(...), which is the same thing the await keyword does, which makes it work as well.
about 4 years ago · Santiago Trujillo 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