Sunday, May 3, 2020

Nodejs MongoDB Delete document


Node.js MongoDB





Deleting the documents


We can use deleteOne() or deleteMany() methods to delete the documents from a MongoDB database collection.

Delete Document


To delete a document as db.collection.deleteOne() method is called. This method deletes only one document. The first parameter of the deleteOne() method is a query object that defines the document to be deleted.

db.collection.deleteOne(
   <filter>,
   {
      writeConcern: <document>,
      collation: <document>
   }
)


  • The filter is a document provides deletion criteria using query operators. An empty document { } can be to delete the first document in the collection.
  • writeConcern document is used to represent the write concern and it is optional.
  • collation is a document used to provide the collation for the operation.

If the query finds multiple documents, then the first document will be deleted only.

(if you haven't idea about Node.js MongoDB create a collection, insert data and find data see previous tutorials)


For example,

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

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
var query={};//delete first document
dbo.collection("employee").deleteOne(query,function(error, obj) {
    if (error) throw error;
    console.log('First document deleted successfully');
    database.close();
  });
});

Output:
First document deleted successfully

deleting a document by field name.

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

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
var query={name:'Krish'};//delete by field document
dbo.collection("employee").deleteOne(query,function(error, obj) {
    if (error) throw error;
    console.log(obj.result.n+' documents deleted successfully');
    database.close();
  });
});

Deleting Many documents


db.collection.deleteMany() method can be used to remove all the documents found by the query.

db.collection.deleteMany(
   <filter>,
   {
      writeConcern: <document>,
      collation: <document>
   }
)


  • The filter is a document provides deletion criteria using query operators. An empty document { } will delete all the documents in the collection.
  • writeConcern document is used to represent the write concern and it is optional.
  • collation is a document used to provide the collation for the operation.

If the query finds multiple documents, then all the documents will be deleted.

For example,

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

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
var query={salary:{$gte:'13000'}};//delete more than one documents
dbo.collection("employee").deleteMany(query,function(error, obj) {
    if (error) throw error;
    console.log(obj.result.n+' documents deleted successfully');
    database.close();
  });
});

Output:
4 documents deleted successfully

Reference: