ArangoCollection interface provides 'deleteDocuments' method to delete multiple documents from the collection.
Signature
MultiDocumentEntity<DocumentDeleteEntity<Void>> deleteDocuments(Collection<?> values) throws ArangoDBException;
<T> MultiDocumentEntity<DocumentDeleteEntity<T>> deleteDocuments(Collection<?> values, Class<T> type, DocumentDeleteOptions options) throws ArangoDBException;
'values' specifies the keys of the documents or the documents themselves.
'DocumentDeleteOptions' is used to specifiy additional options like waitForSync, returnOld, silent etc.,
Example
collection.deleteDocuments(docKeysToDelete);
Find the below working application.
DocumentBulkDelete.java
package com.sample.app;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import com.arangodb.ArangoCollection;
import com.arangodb.ArangoDB;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.DocumentCreateEntity;
import com.arangodb.entity.MultiDocumentEntity;
import com.arangodb.mapping.ArangoJack;
import com.arangodb.model.DocumentCreateOptions;
public class DocumentBulkDelete {
private static final String USER_NAME = "root";
private static final String PASSWORD = "tiger";
private static final String HOST = "127.0.0.1";
private static final int PORT = 8529;
public static void main(String args[]) {
// Get an ArangoDB instance
ArangoDB arangoDB = new ArangoDB.Builder().user(USER_NAME).password(PASSWORD).host(HOST, PORT)
.serializer(new ArangoJack()).build();
String databaseName = "testdb";
arangoDB.createDatabase(databaseName);
ArangoDatabase arangoDatabase = arangoDB.db(databaseName);
String collectionName = "test1";
arangoDatabase.createCollection(collectionName);
ArangoCollection collection = arangoDatabase.collection(collectionName);
BaseDocument empDocument1 = new BaseDocument();
empDocument1.addAttribute("firstName", "Krishna");
empDocument1.addAttribute("lastName", "Gurram");
empDocument1.addAttribute("hobbies", Arrays.asList("trekking", "playing cricket"));
BaseDocument empDocument2 = new BaseDocument();
empDocument2.addAttribute("firstName", "Joel");
empDocument2.addAttribute("lastName", "Chelli");
empDocument2.addAttribute("hobbies", Arrays.asList("designing games"));
DocumentCreateOptions docOptions = new DocumentCreateOptions();
docOptions.returnNew(true);
MultiDocumentEntity<DocumentCreateEntity<BaseDocument>> persistedDocs = collection
.insertDocuments(Arrays.asList(empDocument1, empDocument2), docOptions);
Collection<DocumentCreateEntity<BaseDocument>> docs = persistedDocs.getDocuments();
List<String> docKeysToDelete = new ArrayList<>();
for (DocumentCreateEntity<BaseDocument> doc : docs) {
System.out.println("\n");
System.out.println("id : " + doc.getId());
System.out.println("key : " + doc.getKey());
System.out.println("rev : " + doc.getRev());
System.out.println("doc : " + doc.getNew());
docKeysToDelete.add(doc.getKey());
}
System.out.println("Deleting the documents\n");
collection.deleteDocuments(docKeysToDelete);
for (String key : docKeysToDelete) {
if (collection.documentExists(key)) {
System.out.println("Document is not deleted for the key " + key);
} else {
System.out.println("Document is not exist with the key " + key);
}
}
// Dropping the collection and database
collection.drop();
arangoDatabase.drop();
System.exit(0);
}
}
Output
id : test1/62313 key : 62313 rev : _cS5XKHS--- doc : BaseDocument [documentRevision=_cS5XKHS---, documentHandle=test1/62313, documentKey=62313, properties={firstName=Krishna, lastName=Gurram, hobbies=[trekking, playing cricket]}] id : test1/62314 key : 62314 rev : _cS5XKHS--A doc : BaseDocument [documentRevision=_cS5XKHS--A, documentHandle=test1/62314, documentKey=62314, properties={firstName=Joel, lastName=Chelli, hobbies=[designing games]}] Deleting the documents Document is not exist with the key 62313 Document is not exist with the key 62314
Previous Next Home
No comments:
Post a Comment