Using a Java Client with MongoDB: Deleting Data in MongoDB

In this section we shall delete documents using the DeleteDBDocument application. The MongoCollection<TDocument> interface provides several methods for deleting documents as discussed in Table 1-10.

  1. In the DeleteDBDocument application create a MongoClient client as discussed previously. Create a MongoDatabase instance for the local database from the MongoClient instance and create a MongoCollection<TDocument> instance for the catalog collection from the MongoDatabase instance.

MongoClient mongoClient = new MongoClient(Arrays.asList(new

ServerAddress(“localhost”, 27017)));

MongoDatabase db = mongoClient.getDatabase(“local”);

MongoCollection<Document> coll = db.getCollection(“catalog”);

  1. Create and add four Document instances using the model class Catalog to set the Document fields.

Document catalog = new Document(“catalogId”, “catalog1”)

.append(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”)

.append(“title”, “Engineering as a Service”)

.append(“author”, “David A. Kelly”);

coll.insertOne(catalog);

catalog = new Document(“catalogId”, “catalog2”)

.append(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”)

.append(“title”, “Quintessential and Collaborative”)

.append(“author”, “Tom Haunert”);

coll.insertOne(catalog);

catalog = new Document(“catalogId”, “catalog3”)

.append(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”);

coll.insertOne(catalog);

catalog = new Document(“catalogId”, “catalog4”)

.append(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”);

coll.insertOne(catalog);

  1. As an example of using the deleteOne(Bson filter) method delete the document with catalogId as catalog1.

DeleteResult result = coll.deleteOne(new Document(“catalogId”, “catalog1”));

  1. The deleteOne() method returns a DeleteResult object. Output the number of documents deleted using the getDeletedCount() method of DeleteResult.

System.out.println(“Number of documents deleted: “

+ result.getDeletedCount());

  1. As an example of using the findOneAndDelete(Bson filter) method delete the document with catalogId as catalog2. The findOneAndDelete() method returns the document deleted. Output the deleted document.

Document documentDeleted = coll.findOneAndDelete(new

Document(“catalogId”, “catalog2”));

System.out.println(“Document deleted: ” + documentDeleted);

  1. As an example of using the deleteMany(Bson filter) method delete all the remaining documents by providing a Document object without a specified key/value pair as a method argument. Subsequently, output the number of documents deleted using the getDeletedCount() method in DeleteResult.

DeleteResult result = coll.deleteMany(new Document());

System.out.println(“Number of documents deleted: “+ result.

getDeletedCount());

  1. To verify that the document/s got deleted, find all the documents using the find() method and output the key/value pairs in each of the documents as discussed before. The DeleteDBDocument application is listed below.

package mongodb;

import java.util.Arrays;

import java.util.Iterator;

import java.util.Set;

import org.bson.Document;

import com.mongodb.MongoClient;

import com.mongodb.ServerAddress;

import com.mongodb.client.FindIterable;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;

import com.mongodb.client.result.DeleteResult;

public class DeleteDBDocument {

public static void main(String[] args) {

MongoClient mongoClient = new MongoClient(

Arrays.asList(new ServerAddress(“localhost”, 27017)));

MongoDatabase db = mongoClient.getDatabase(“local”);

MongoCollection<Document> coll = db.getCollection(“catalog”);

Document catalog = new Document(“catalogId”, “catalog1”)

.append(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”)

.append(“title”, “Engineering as a Service”)

.append(“author”, “David A. Kelly”);

coll.insertOne(catalog);

catalog = new Document(“catalogId”, “catalog2”)

.append(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”)

.append(“title”, “Quintessential and Collaborative”)

.append(“author”, “Tom Haunert”);

coll.insertOne(catalog);

catalog = new Document(“catalogId”, “catalog3”)

.append(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”);

coll.insertOne(catalog);

catalog = new Document(“catalogId”, “catalog4”)

.append(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”);

coll.insertOne(catalog);

DeleteResult result = coll.deleteOne(

new Document(“catalogId”, “catalog1”));

System.out.println(“Number of documents deleted: “

+ result.getDeletedCount());

Document documentDeleted = coll

.findOneAndDelete(new Document(“catalogId”, “catalog2”));

System.out.println(“Document deleted: ” + documentDeleted);

result = coll.deleteMany(new Document());

System.out.println(“Number of documents deleted: “

+ result.getDeletedCount());

FindIterable<Document> iterable = coll.find();

String documentKey = null;

for (Document document : iterable) {

Set<String> keySet = document.keySet();

Iterator<String> iter = keySet.iterator(); while (iter.hasNext()) {

documentKey = iter.next();

System.out.println(documentKey);

System.out.println(document.get(documentKey));

}

}

mongoClient.close();

}

}

  1. Before running the application drop the catalog collection with db.catalog.drop() command in mongo shell. To run the DeleteDBDocument application, right-click on the DeleteDBDocument.java file in Package Explorer and select Run As ► Java Application as shown in Figure 1-18.

The output from DeleteDBDocument.java application is shown in Figure 1-19.

As the deleteOne() method deletes one document, the number of documents indicated to have been deleted subsequent to the invocation of the deleteOne() method is output as 1. The document deleted with the findOneAndDelete() method is output. The delete count for the deleteMany() method is output as 2, which is the number of documents in the catalog collection after having deleted 2 of the 4 documents added in the DeleteDBDocument application.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *