Ofek Tsoref
Ofek Tsoref

Reputation: 19

How to delete in mongoDB - nodejs

I'm doing a small-scale school project where I need to 'create an app' that manages a list of contacts. I'm really new to Nodejs and mongodb and sadly I cannot find any answer in the internet.

I'm trying to delete a record from a document, by debugging I know that the data (id) is sent correctly to the server.

 router.delete('/contact/:id', (req, res, next) => {
    db.contacts.deleteOne
       ({_id: mongojs.ObjectId(req.params.id)
    }).then(res => {
        console.log('deleted ' + res);
        res.status(200)
        .json({message: 'Deletion successful! '})
    });
});

Thank you very much :)

Upvotes: 0

Views: 98

Answers (1)

Joe25
Joe25

Reputation: 136

From your code, I guess you're using mongojs, mongojs doesn't contain a deleteOne method, so you have to rely on the provided API for that purpose by using :

db.collection.remove(query, [justOne], [callback])

More details on mongoJs official documentation Here

Upvotes: 2

Related Questions