【发布时间】:2019-11-21 16:02:30
【问题描述】:
我正在使用 azure 的 Cosmos db 和使用 python 的 SQL API。
我已经将一些数据上传到 cosmos db,但现在我想删除它。
我在这个问题中看到了怎么做:Cosmos DB - Delete Document with Python
但对我来说不起作用,这是我的代码:
config = {
'ENDPOINT': "ENDPOINT",
'MASTERKEY': 'key',
'DOCUMENTDB_DATABASE': 'database',
'DOCUMENTDB_COLLECTION': 'collection'
};
# Initialize the Python DocumentDB client
client = document_client.DocumentClient(config['ENDPOINT'], {'masterKey': config['MASTERKEY']})
# use a SQL based query to get a bunch of documents
query = { 'query': 'SELECT * FROM c' }
options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = -1
options['partitionKey'] = '2017'
result_iterable = client.QueryDocuments('dbs/database/colls/collection', query, options)
results = list(result_iterable);
print(results)
#client.DeleteDocument('dbs/ToDoList/colls/nc4data/docs/'+result_iterableID,options)
for x in range(0, len (results)):
docID = results[x]['id']
print (docID)
client.DeleteDocument('dbs/database/colls/collection/docs/'+docID, options=options)
print ('deleted', docID)
#print ('delete success')
我的分区键是年份,从 2016 年到 2019 年。我尝试将选项中的分区键作为年份之一,正如我上面链接的答案:
options['partitionKey'] = '2017'
我也尝试过这样做:
options['partitionKey'] = 2017 (year is string, but I was trying)
options['partitionKey'] = 'year'
options['partitionKey'] = '/year'
当我在我的选项字典中插入这个“partitionKey”时,我在结果中获得了一个 empy 列表。
我也试过没有这个'partitionKey',然后我得到所有数据,但我得到这个错误:
{\"Errors\":[\"x-ms-partitionkey 标头中提供的分区键的组件数少于集合中定义的组件数。
【问题讨论】:
标签: python azure-cosmosdb