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

165
Vistas
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 Respuestas
Responde la pregunta

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 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