Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!