After getting the document, specify the document ID and PartitionKey and delete it.
// Azure Cosmos DB Libraries for Java
// https://docs.microsoft.com/ja-jp/java/api/overview/azure/cosmosdb?view=azure-java-stable
FeedOptions queryOptions = new FeedOptions();
queryOptions.setEnableCrossPartitionQuery(true);
String host = "yourhost";
// Get key from Azure Web Console
// read write key
String key = "yourkey";
DocumentClient client = new DocumentClient("https://" //
+ host //
+ ".documents.azure.com:443", key, //
new ConnectionPolicy(), ConsistencyLevel.Session);
String collectionLink = String.format("/dbs/%s/colls/%s", DATABASE_ID, COLLECTION_ID); //Specify appropriately
String q = "SELECT * FROM container1";
//All documents
List<Document> results = client //
.queryDocuments(collectionLink, q, queryOptions).getQueryIterable().toList();
for (Document doc : results) {
System.err.println(doc);
String documentLink = doc.getSelfLink();
RequestOptions options = new RequestOptions();
// check your configuration of cosmos db container
String partitionKey = ((org.json.JSONObject) doc.get("item")).getString("xxx");
options.setPartitionKey(new PartitionKey(partitionKey));
client.deleteDocument(documentLink, options);
System.err.println("deleted: " + documentLink);
}
client.close();
You must specify the value, not the Patition key. Since "SELECT * FROM c" can be done, it seems that "DELETE * FROM c" can be done, but it seems that it cannot be done.
Recommended Posts