根据我的经验,如果您的集合没有分区密钥,则在操作数据库时无需设置分区密钥的查询标准。
在您的帖子中,该集合没有分区键,并且您将分区键设置为RequestOption。因此,数据库当然不知道在哪里可以找到要运行的文档。
您可以参考我的代码段:
import com.microsoft.azure.documentdb.*;
public class DeleteDocuments {
private static String accountName="https://jay.documents.azure.com:443/";
private static String accountKey="Czi66skfjZYLTaXuDhoxNb2JHL4DR98VxAxGXtLkWFnjCa5e7gUXQuPgemlXwyPWjjWJpwrseH1wPMfhkqA8cQ==";
private static String databaseName = "db";
private static String collectionName = "coll";
public static void main(String[] args) throws DocumentClientException {
DocumentClient client = new DocumentClient(
accountName,
accountKey
, new ConnectionPolicy(),
ConsistencyLevel.Session);
FeedOptions options = null;
String sql = "select * from c";
FeedResponse<Document> queryResults = client.queryDocuments("dbs/"+databaseName+"/colls/"+collectionName, sql, options);
System.out.println("before delete :");
for (Document d : queryResults.getQueryIterable()) {
System.out.println(String.format("\tRead %s", d));
}
RequestOptions requestOptions = new RequestOptions();
requestOptions.setOfferThroughput(400);
client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/1",requestOptions);
queryResults = client.queryDocuments("dbs/"+databaseName+"/colls/"+collectionName, sql, options);
System.out.println("after delete :");
for (Document d : queryResults.getQueryIterable()) {
System.out.println(String.format("\tRead %s", d));
}
}
}
更新答案:
我认为你误解了partitionkey
财产在options[]
.
例如,我的容器是这样创建的:
![enter image description here](https://i.stack.imgur.com/BtJLA.png)
分区密钥是我在这里收藏的“名称”。您可以查看收集的分区密钥。
和我的文件如下:
{
"id": "1",
"name": "jay"
}
{
"id": "2",
"name": "jay2"
}
My partitionkey
is 'name',所以在这里我有两个分区:'jay' and 'jay1'.
所以,这里你应该设置partitionkey
属性为“jay”或“jay2”,而不是“name”。
此时,如果我运行下面的代码而不将分区键设置到 RequestOptions 中,我将遇到与您相同的问题。
RequestOptions requestOptions = new RequestOptions();
requestOptions.setOfferThroughput(400);
client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/1",requestOptions);
线程“main”中的异常 java.lang.UnsupportedOperationException:
必须为此操作提供 PartitionKey 值。在
com.microsoft.azure.documentdb.DocumentClient.addPartitionKeyInformation(DocumentClient.java:3199)
在
com.microsoft.azure.documentdb.DocumentClient.addPartitionKeyInformation(DocumentClient.java:3180)
在
com.microsoft.azure.documentdb.DocumentClient.deleteDocument(DocumentClient.java:959)
在DeleteDocuments.main(DeleteDocuments.java:32)
我需要将分区键参数设置为所操作文档存储的分区。
RequestOptions requestOptions = new RequestOptions();
requestOptions.setOfferThroughput(400);
PartitionKey partitionKey = new PartitionKey("jay");
requestOptions.setPartitionKey(partitionKey);
client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/1",requestOptions);
更新答案2:
我猜你想操作没有设置分区键的文件。
请参考这个完美blog,你会找到答案的!
在java代码中,只需将分区键设置为Undefined.Value()
然后一切都会完成。
RequestOptions requestOptions = new RequestOptions();
requestOptions.setOfferThroughput(400);
PartitionKey partitionKey = new PartitionKey(Undefined.Value());
requestOptions.setPartitionKey(partitionKey);
client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/3",requestOptions);
希望对您有帮助。