通过Query Explorer从条件中删除CosmosDB中的文档

时间:2017-06-27 07:46:29

标签: azure azure-cosmosdb

查询或其他一些快速方法删除与集合中的where 条件相匹配的所有文档是什么?
 我想要像DELETE * FROM c WHERE c.DocumentType = 'EULA'这样的东西,但显然它不起作用。

注意:我不是在寻找任何C#实现。

5 个答案:

答案 0 :(得分:11)

  

我想要一些像DELETE * FROM c WHERE c.DocumentType =' EULA'   但是,显然,它不起作用。

不支持以这种方式删除文档。您需要先使用SELECT查询选择文档,然后单独删除它们。如果你愿意,你可以编写提取和代码的代码。删除存储过程,然后执行该存储过程。

答案 1 :(得分:7)

这有点旧,但只是有相同的要求,并找到了@Gaurav Mantri所写的具体例子。

存储过程脚本在这里:

https://social.msdn.microsoft.com/Forums/azure/en-US/ec9aa862-0516-47af-badd-dad8a4789dd8/delete-multiple-docdb-documents-within-the-azure-portal?forum=AzureDocumentDB

转到Azure门户,从上面抓取脚本,然后在需要删除的database->集合中创建一个新的存储过程。

然后在存储过程窗格的底部,脚本textarea下面是放入参数的位置。在我的情况下,我只想删除所有我使用的:

SELECT c._self FROM c

我想你的是:

SELECT c._self FROM c WHERE c.DocumentType = 'EULA'

然后点击“保存并执行”。 Viola,一些文件被删除。在Azure门户中工作之后,我切换了Azure DocumentDB Studio,更好地了解了发生的情况。即我可以看到我被限制删除18次(在结果中返回)。出于某种原因,我无法在Azure门户中看到这一点。

无论如何,即使每次执行仅限于一定数量的删除,也非常方便。执行sp也会受到限制,因此您不能仅仅粘贴键盘。我想我会删除并重新创建Collection,除非我有一个可管理的文件要删除(思考< 500)。

向Mimi Gentz @Microsoft道具,以便在上面的链接中分享脚本。

HTH

答案 2 :(得分:3)

我写了一个脚本来列出所有文档并删除所有文档,也可以对其进行修改以删除选定的文档。

https://www.lvguowei.me/post/azure-cosmosdb-clear/

答案 3 :(得分:1)

如果你想在 C#/Dotnet Core 中实现,这个项目可能会有所帮助:https://github.com/lokijota/CosmosDbDeleteDocumentsByQuery。这是一个简单的 Visual Studio 项目,您可以在其中指定 SELECT 查询,并且所有匹配项都将 a) 备份到文件; b) 删除,基于一组标志。

答案 4 :(得分:0)

##### Here is the python script which can be used to delete data from Partitioned Cosmos Collection #### This will delete documents Id by Id based on the result set data.

Identify the data that needs to be deleted before below step

res_list = "select id from id_del"
res_id = [{id:x["id"]} 
             for x in sqlContext.sql(res_list).rdd.collect()]
config = {
   "Endpoint" : "Use EndPoint"
  "Masterkey" : "UseKey", 
      "WritingBatchSize" : "5000",
    'DOCUMENTDB_DATABASE': 'Database',
    'DOCUMENTDB_COLLECTION': 'collection-core'
}; 

for row in res_id:
# Initialize the Python DocumentDB client
  client = document_client.DocumentClient(config['Endpoint'], {'masterKey': config['Masterkey']})

# use a SQL based query to get   documents

## Looping thru partition to delete

  query = { 'query': "SELECT c.id FROM c where c.id = "+ "'" +row[id]+"'"   }
  print(query)
  options = {}
  options['enableCrossPartitionQuery'] = True
  options['maxItemCount'] = 1000
  result_iterable = client.QueryDocuments('dbs/Database/colls/collection-core', query, options)
  results = list(result_iterable)
  print('DOCS TO BE DELETED : ' + str(len(results)))
  if len(results) > 0 :
      for i in range(0,len(results)):
      #  print(results[i]['id'])
          docID = results[i]['id']
          print("docID :" + docID)
          options = {}
          options['enableCrossPartitionQuery'] = True
          options['maxItemCount'] = 1000
          options['partitionKey'] = docID
          client.DeleteDocument('dbs/Database/colls/collection-core/docs/'+docID,options=options)
          print ('deleted Partition:' +  docID)