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

160
Visualizações
Is new keyword is necessary with ObjectID in MongoDB Query

I was working on a Typescript web project that used MongoDB and ExpressJS. I used a MongoDB query to find and delete a document from MongoDB using the HTTP DELETE method. During the MongoDB query, I got mixed up with a term named new and ObjectID. Here are the code snippets:

//Without new Keyword
      const resData: DeleteWriteOpResultObject = await data
        .collection("posts")
        .deleteOne({
          _id: ObjectID(req.params.id),
        });


//With new keyword
  const resData: DeleteWriteOpResultObject = await data
    .collection("posts")
    .deleteOne({
      _id: new ObjectID(req.params.id),
    });

Both are working fine as I expected. But I want to know what's the difference between these two. Is there any memory-related difference or something else?

While googling I got an article also. I am attaching the link here.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to the official driver documentation, ObjectId is a class which means you need to use new to instantiate it.

However, looking at the actual implementation, it will allow itself to be called as a regular function, in which case it will return new ObjectId(...) (so it will instantiate itself automatically).

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