Cosmos DB-使用Python删除文档 [英] Cosmos DB - Delete Document with Python
问题描述
在此SO问题中,我了解到无法删除Cosmos DB文档使用SQL.
使用Python,我相信我需要DeleteDocument()
方法.这就是我获取所需的文档ID的方式(我相信),然后再调用DeleteDocument()
方法.
# set up the client
client = document_client.DocumentClient()
# use a SQL based query to get a bunch of documents
query = { 'query': 'SELECT * FROM server s' }
result_iterable = client.QueryDocuments('dbs/DB/colls/coll', query, options)
results = list(result_iterable);
for x in range(0, len (results)):
docID = results[x]['id']
现在,在此阶段我想呼叫DeleteDocument()
.
输入的是document_link
和options
.
我可以将document_link
定义为类似
document_link = 'dbs/DB/colls/coll/docs/'+docID
并成功调用例如与DeleteDocument()
相同输入的ReadAttachments()
.
但是,当我这样做时,我会得到一个错误...
The partition key supplied in x-ms-partitionkey header has fewer
components than defined in the the collection
...现在我完全迷路了
更新
在杰伊的帮助下,我相信我在选项中缺少了partitonKey
元素.
在此示例中,我创建了一个测试数据库,它看起来像这样
所以我认为我的分区键是/testPART
但是,当我在选项中包含partitionKey
时,不会返回任何结果(因此print len(results)
输出0).
删除partitionKey
表示返回结果,但是删除尝试仍然失败.
# Query them in SQL
query = { 'query': 'SELECT * FROM c' }
options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 2
options['partitionKey'] = '/testPART'
result_iterable = client.QueryDocuments('dbs/testDB/colls/testCOLL', query, options)
results = list(result_iterable)
# should be > 0
print len(results)
for x in range(0, len (results)):
docID = results[x]['id']
print docID
client.DeleteDocument('dbs/testDB/colls/testCOLL/docs/'+docID, options=options)
print 'deleted', docID
根据您的描述,我尝试使用 请注意,如果文档为 对于需要跨所有查询执行的任何查询,必须将其设置为true
一个以上的分区.这是一个明确的标志,使您能够
在开发期间进行有意识的性能折衷. 您可以在此处. 更新答案: 我认为您误解了 例如,我的容器是这样创建的: 我的文档如下: 我的 因此,在这里,您应该将 请如下修改您的代码: 希望它对您有帮助. In this SO question I had learnt that I cannot delete a Cosmos DB document using SQL. Using Python, I believe I need the Now, at this stage I want to call I can define And successfully call When I do however, I get an error... ...and now I'm totally lost UPDATE Following on from Jay's help, I believe I'm missing the In this example, I've created a testing database, it looks like this
So I think my partition key is When I include the Removing
According to your description, I tried to use pydocument module to delete document in my Here is my code: Console Result: Please notice that you need to set the Must be set to true for any query that requires to be executed across
more than one partition. This is an explicit flag to enable you to
make conscious performance tradeoffs during development time. You could find above description from here. Update Answer: I think you misunderstand the meaning of For example , my container is created like this: My documents as below : My So, here you should set the Please modify your code as below: Hope it helps you. 这篇关于Cosmos DB-使用Python删除文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!cross-partitioned
,则需要在options
中将enableCrossPartitionQuery
属性设置为 True .
options[]
中partitionkey
属性的含义. {
"id": "1",
"name": "jay"
}
{
"id": "2",
"name": "jay2"
}
partitionkey
是'name',所以这里有两个分区:'jay'和'jay1'.>
partitionkey
属性设置为'jay'或'jay2',而不是'name'. options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 2
options['partitionKey'] = 'jay' (please change here in your code)
result_iterable = client.QueryDocuments('dbs/db/colls/testcoll', query, options)
results = list(result_iterable);
print(results)
DeleteDocument()
method. This is how I'm getting the document ID's that are required (I believe) to then call the DeleteDocument()
method. # set up the client
client = document_client.DocumentClient()
# use a SQL based query to get a bunch of documents
query = { 'query': 'SELECT * FROM server s' }
result_iterable = client.QueryDocuments('dbs/DB/colls/coll', query, options)
results = list(result_iterable);
for x in range(0, len (results)):
docID = results[x]['id']
DeleteDocument()
.
The inputs into which are document_link
and options
.document_link
as something likedocument_link = 'dbs/DB/colls/coll/docs/'+docID
ReadAttachments()
for example, which has the same inputs as DeleteDocument()
.The partition key supplied in x-ms-partitionkey header has fewer
components than defined in the the collection
partitonKey
element in the options./testPART
partitionKey
in the options however, no results are returned, (and so print len(results)
outputs 0).partitionKey
means that results are returned, but the delete attempt fails as before.# Query them in SQL
query = { 'query': 'SELECT * FROM c' }
options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 2
options['partitionKey'] = '/testPART'
result_iterable = client.QueryDocuments('dbs/testDB/colls/testCOLL', query, options)
results = list(result_iterable)
# should be > 0
print len(results)
for x in range(0, len (results)):
docID = results[x]['id']
print docID
client.DeleteDocument('dbs/testDB/colls/testCOLL/docs/'+docID, options=options)
print 'deleted', docID
azure document db
and it works for me.import pydocumentdb;
import pydocumentdb.document_client as document_client
config = {
'ENDPOINT': 'Your url',
'MASTERKEY': 'Your master key',
'DOCUMENTDB_DATABASE': 'familydb',
'DOCUMENTDB_COLLECTION': 'familycoll'
};
# 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 server s' }
options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 2
result_iterable = client.QueryDocuments('dbs/familydb/colls/familycoll', query, options)
results = list(result_iterable);
print(results)
client.DeleteDocument('dbs/familydb/colls/familycoll/docs/id1',options)
print 'delete success'
[{u'_self': u'dbs/hitPAA==/colls/hitPAL3OLgA=/docs/hitPAL3OLgABAAAAAAAAAA==/', u'myJsonArray': [{u'subId': u'sub1', u'val': u'value1'}, {u'subId': u'sub2', u'val': u'value2'}], u'_ts': 1507687788, u'_rid': u'hitPAL3OLgABAAAAAAAAAA==', u'_attachments': u'attachments/', u'_etag': u'"00002100-0000-0000-0000-59dd7d6c0000"', u'id': u'id1'}, {u'_self': u'dbs/hitPAA==/colls/hitPAL3OLgA=/docs/hitPAL3OLgACAAAAAAAAAA==/', u'myJsonArray': [{u'subId': u'sub3', u'val': u'value3'}, {u'subId': u'sub4', u'val': u'value4'}], u'_ts': 1507687809, u'_rid': u'hitPAL3OLgACAAAAAAAAAA==', u'_attachments': u'attachments/', u'_etag': u'"00002200-0000-0000-0000-59dd7d810000"', u'id': u'id2'}]
delete success
enableCrossPartitionQuery
property to True in options
if your documents are cross-partitioned
.
partitionkey
property in the options[]
. {
"id": "1",
"name": "jay"
}
{
"id": "2",
"name": "jay2"
}
partitionkey
is 'name', so here I have two paritions : 'jay' and 'jay1'.partitionkey
property to 'jay' or 'jay2',not 'name'. options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 2
options['partitionKey'] = 'jay' (please change here in your code)
result_iterable = client.QueryDocuments('dbs/db/colls/testcoll', query, options)
results = list(result_iterable);
print(results)