Delete by id mongodb node js

Why I can't remove record by _id?

Code:

db.collection('posts', function(err, collection) {
   collection.remove({_id: '4d512b45cc9374271b00000f'});
});

JohnnyHK

295k63 gold badges603 silver badges457 bronze badges

asked Oct 15, 2012 at 18:17

You need to pass the _id value as an ObjectID, not a string:

var mongodb = require('mongodb');

db.collection('posts', function(err, collection) {
   collection.deleteOne({_id: new mongodb.ObjectID('4d512b45cc9374271b00000f')});
});

Delete by id mongodb node js

answered Oct 15, 2012 at 18:39

JohnnyHKJohnnyHK

295k63 gold badges603 silver badges457 bronze badges

3

MongoDb has now marked the remove method as deprecated. It has been replaced by two separate methods: deleteOne and deleteMany.

Here is their relevant getting started guide: https://docs.mongodb.org/getting-started/node/remove/

and here is a quick sample:

var mongodb = require('mongodb');

db.collection('posts', function(err, collection) {
   collection.deleteOne({_id: new mongodb.ObjectID('4d512b45cc9374271b00000f')}, function(err, results) {
       if (err){
         console.log("failed");
         throw err;
       }
       console.log("success");
    });
});

answered Mar 14, 2016 at 7:31

Bill TarbellBill Tarbell

4,6282 gold badges32 silver badges49 bronze badges

With TypeScript, you can to it using imports, instead of requiring the whole library

import { ObjectID } from 'mongodb'   

 

db.collection('posts', function(err, collection) {
   collection.deleteOne({_id: new ObjectID('4d512b45cc9374271b00000f')});
});

answered Dec 19, 2020 at 6:21

Delete by id mongodb node js

Len JosephLen Joseph

1,2649 silver badges20 bronze badges

First include mongodb

var mongodb = require("mongodb");

You have to include the ObjectID from mongodb

var ObjectID = require('mongodb').ObjectID;

Then Use

var delete_id = request.params.id;//your id

 collection.deleteOne({_id: new mongodb.ObjectID(delete_id.toString())});

1000% works...

answered Dec 9, 2020 at 16:45

Delete by id mongodb node js

i think we have to require mongodb as const and use it with mongodb

answered Sep 7, 2021 at 10:43

I recently stumbled with this problem today and I find that the fix is:

const mongodb = require('mongodb');
const ObjectID = require('mongodb').ObjectID;
databaseName.collectionName.deleteOne({_id: new mongodb.ObjectID(id)} , (err)=>{
   if (err) throw err;
   console.log('Deleted'+id);
});

answered Feb 3 at 15:41

1


Delete Document

To delete a record, or document as it is called in MongoDB, we use the deleteOne() method.

The first parameter of the deleteOne() method is a query object defining which document to delete.

Note: If the query finds more than one document, only the first occurrence is deleted.

Example

Delete the document with the address "Mountain 21":

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  var myquery = { address: 'Mountain 21' };
  dbo.collection("customers").deleteOne(myquery, function(err, obj) {
    if (err) throw err;
    console.log("1 document deleted");
    db.close();
  });
});

Run example »

Save the code above in a file called "demo_delete.js" and run the file:

Run "demo_delete.js"

C:\Users\Your Name>node demo_delete.js

Which will give you this result:



Delete Many

To delete more than one document, use the deleteMany() method.

The first parameter of the deleteMany() method is a query object defining which documents to delete.

Example

Delete all documents were the address starts with the letter "O":

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  var myquery = { address: /^O/ };
  dbo.collection("customers").deleteMany(myquery, function(err, obj) {
    if (err) throw err;
    console.log(obj.result.n + " document(s) deleted");
    db.close();
  });
});

Run example »

Save the code above in a file called "demo_delete_many.js" and run the file:

Run "demo_delete_many.js"

C:\Users\Your Name>node demo_delete_many.js

Which will give you this result:


The Result Object

The deleteMany() method returns an object which contains information about how the execution affected the database.

Most of the information is not important to understand, but one object inside the object is called "result" which tells us if the execution went OK, and how many documents were affected.

The result object looks like this:

You can use this object to return the number of deleted documents:

Example

Return the number of deleted documents:

console.log(obj.result.n);

Which will produce this result: