Cosmos DB-使用Python删除文档 [英] Cosmos DB - Delete Document with Python

查看:81
本文介绍了Cosmos DB-使用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_linkoptions.

我可以将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

解决方案

根据您的描述,我尝试使用

请注意,如果文档为cross-partitioned,则需要在options中将enableCrossPartitionQuery属性设置为 True .

对于需要跨所有查询执行的任何查询,必须将其设置为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)

希望它对您有帮助.

In this SO question I had learnt that I cannot delete a Cosmos DB document using SQL.

Using Python, I believe I need the 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']

Now, at this stage I want to call DeleteDocument(). The inputs into which are document_link and options.

I can define document_link as something like

document_link = 'dbs/DB/colls/coll/docs/'+docID

And successfully call ReadAttachments() for example, which has the same inputs as DeleteDocument().

When I do however, I get an error...

The partition key supplied in x-ms-partitionkey header has fewer
components than defined in the the collection

...and now I'm totally lost

UPDATE

Following on from Jay's help, I believe I'm missing the partitonKey element in the options.

In this example, I've created a testing database, it looks like this

So I think my partition key is /testPART

When I include the partitionKey in the options however, no results are returned, (and so print len(results) outputs 0).

Removing 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

解决方案

According to your description, I tried to use pydocument module to delete document in my azure document db and it works for me.

Here is my code:

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'

Console Result:

[{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

Please notice that you need to set the enableCrossPartitionQuery property to True in options if your documents are cross-partitioned.

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 partitionkey property in the options[].

For example , my container is created like this:

My documents as below :

{
    "id": "1",
    "name": "jay"
}

{
    "id": "2",
    "name": "jay2"
}

My partitionkey is 'name', so here I have two paritions : 'jay' and 'jay1'.

So, here you should set the partitionkey property to 'jay' or 'jay2',not 'name'.

Please modify your code as below:

 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)

Hope it helps you.

这篇关于Cosmos DB-使用Python删除文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆