Delete Documents from CosmosDB based on condition through Query Explorer

后端 未结 4 1371
天涯浪人
天涯浪人 2021-02-18 14:56

What\'s the query or some other quick way to delete all the documents matching the where condition in a collection?
I want something like DELETE * FROM c WHE

4条回答
  •  半阙折子戏
    2021-02-18 15:40

    ##### 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)
    

提交回复
热议问题