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

186
Views
How I can remove only one object in mongodb, not all objects matching the filter query?

This is my code for deleting object containg name as 'bob':

app.delete('/user/:id', async (req, res) => {
            const id = req.params.id;
            const query = { name: "bob" }
            const result = await userCollection.deleteOne(query);
            res.send(result);
 });

This code delete all objects that have name as 'bob', but I want to delete any one instance only from db matching this query, not all objects. And is there any way to set count that how many instance I want to delete matching this query?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use this code

const ObjectId = require("mongodb").ObjectId;
app.delete('/user/:id', async (req, res) => {
            const id = req.params.id;
            const query = { _id: ObjectId(id) };
            const result = await userCollection.deleteOne(query);
            res.send(result);
 });
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!