Delete one: To delete one document from database
deleteone() is a method which is used to delete a document in the database. By default it deletes first document from the database.
If I pass only brackets(i.e. blank query) it will delete the first document present in the collection.
eg:- db.collection(‘collectionname’).deleteOne({ },function(err,result)
If we want to delete any specific document then we will pass a specific query to deleteOne(). As you can see in the example I have mentioned specific record which I want to delete, here it will delete the first document matched to the query I passed to deleteOne().
var MongoClient = require('mongodb').MongoClient;
// Connection url
var url = "mongodb://localhost:27017/mydb";
//Connect using MongoClient
MongoClient.connect(url, function(err, Client) {
if (err) throw err;
//database name(mydb)
var db = Client.db('mydb');
var data = { name:'rohan' };
//collection name customers
db.collection("customers").deleteOne(data, function(err, result) {
if (err) throw err;
console.log("1 document deleted");
Client.close();
});
});
Run command
C:\Users\Your Name>node deleteone.js
Result
1 document delete
Delete many: To Delete multiple documents from Database
deletemany() is a method which is used to delete multiple documents in the database.
In this function if we pass blank query object i.e. { } it will delete all the documents present in that collection.
eg:- db.collection(‘collectionname’).deleteMany({ },function(err,result)
And if we want to delete some specific documents than we will pass a query to documentMany() it will delete all the documents which match the query.
eg:- db.collection(‘collectionname’).deleteMany(query,function(err,result)
In the example below we have mentioned specific record which we want to delete. Method will delete all records with same name.
var MongoClient = require('mongodb').MongoClient;
//Connect url
var url = "mongodb://localhost:27017/mydb";
//Connect using MongoClient
MongoClient.connect(url, function(err, Client) {
if (err) throw err;
//database name
var db = Client.db('mydb');
var data = { name: 'rohan' };
//collection name
db.collection("customers").deleteMany(data, function(err, obj) {
if (err) throw err;
//obj .result show no. of delete document
console.log(obj.result.n + " document(s) deleted");
Client.close();
});
});
Run command
C:\Users\Your Name>node deletemany.js
Result
4 document deletes
Delete a document using Objectid
You can also access ID in node.js file by requiring ObjectId class present in mongodb module. In example below I will delete document using specific ID
var ObjectId = require('mongodb').ObjectId;
var MongoClient = require('mongodb').MongoClient;
//Connect url
var url = "mongodb://localhost:27017/mydb";
//Connect using MongoClient
MongoClient.connect(url, function(err, Client) {
if (err) throw err;
//database name
var db = Client.db('mydb');
var data = { _id: ObjectId("5a44c67ea0401b0be8229a7e") };
//collection name
db.collection("customers").deleteOne(data, function(err, obj) {
if (err) throw err;
console.log(obj);
console.log("1 deleted");
Client.close();
});
});
Run command :-
C:\Users\Your Name>node delete_id.js
Result:-
1 document delete
Learn More: