2017-04-24 37 views
3

什么是查询或其他快速删除集合中所有文档的方法?现在我正在删除整个集合并重新创建。如何通过查询浏览器清除DocumentDB中的集合

+0

AFAIK,有没有其他办法可以做到这一点从门户。 – Mikhail

+0

如果有用,请将其标记为答案,以帮助有更多问题的社区。 –

+0

乌尔答案将帮助用户这就是为什么我upvoted它,但1)你的答案不符合快速简短的方式2)我希望有一些正式的方式来做到这一点(这仍在进展中)...所以我将保持打开状态直至时间 – JerryGoyal

回答

0

目前,您正在执行的方法是通过Azure门户完成此操作的唯一方法。您可能想要考虑使用脚本或编码来实现一个工具来删除集合,而不是删除集合来完成。

+0

保存并执行按钮不工作。它是灰色的,只有“保存”的选项。这很愚蠢。 –

5

什么是查询或其他快速的方式来删除集合中的所有文件?

由于Chris Pietschmann提到它目前在Azure门户上不受支持,而此feature由Azure Documentdb团队查看。

我们可以用服务器端脚本(例如存储过程,udfs,触发器)做到这一点 我从另一个SO thread获得以下代码。它在我身边正常工作。

/** 
* A DocumentDB stored procedure that bulk deletes documents for a given query.<br/> 
* Note: You may need to execute this sproc multiple times (depending whether the sproc is able to delete every document within the execution timeout limit). 
* 
* @function 
* @param {string} query - A query that provides the documents to be deleted (e.g. "SELECT * FROM c WHERE c.founded_year = 2008") 
* @returns {Object.<number, boolean>} Returns an object with the two properties:<br/> 
* deleted - contains a count of documents deleted<br/> 
* continuation - a boolean whether you should execute the sproc again (true if there are more documents to delete; false otherwise). 
*/ 
function bulkDeleteSproc(query) { 
    var collection = getContext().getCollection(); 
    var collectionLink = collection.getSelfLink(); 
    var response = getContext().getResponse(); 
    var responseBody = { 
     deleted: 0, 
     continuation: true 
    }; 

    // Validate input. 
    if (!query) throw new Error("The query is undefined or null."); 

    tryQueryAndDelete(); 

    // Recursively runs the query w/ support for continuation tokens. 
    // Calls tryDelete(documents) as soon as the query returns documents. 
    function tryQueryAndDelete(continuation) { 
     var requestOptions = {continuation: continuation}; 

     var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, retrievedDocs, responseOptions) { 
      if (err) throw err; 

      if (retrievedDocs.length > 0) { 
       // Begin deleting documents as soon as documents are returned form the query results. 
       // tryDelete() resumes querying after deleting; no need to page through continuation tokens. 
       // - this is to prioritize writes over reads given timeout constraints. 
       tryDelete(retrievedDocs); 
      } else if (responseOptions.continuation) { 
       // Else if the query came back empty, but with a continuation token; repeat the query w/ the token. 
       tryQueryAndDelete(responseOptions.continuation); 
      } else { 
       // Else if there are no more documents and no continuation token - we are finished deleting documents. 
       responseBody.continuation = false; 
       response.setBody(responseBody); 
      } 
     }); 

     // If we hit execution bounds - return continuation: true. 
     if (!isAccepted) { 
      response.setBody(responseBody); 
     } 
    } 

    // Recursively deletes documents passed in as an array argument. 
    // Attempts to query for more on empty array. 
    function tryDelete(documents) { 
     if (documents.length > 0) { 
      // Delete the first document in the array. 
      var isAccepted = collection.deleteDocument(documents[0]._self, {}, function (err, responseOptions) { 
       if (err) throw err; 

       responseBody.deleted++; 
       documents.shift(); 
       // Delete the next document in the array. 
       tryDelete(documents); 
      }); 

      // If we hit execution bounds - return continuation: true. 
      if (!isAccepted) { 
       response.setBody(responseBody); 
      } 
     } else { 
      // If the document array is empty, query for more documents. 
      tryQueryAndDelete(); 
     } 
    } 
} 

的更多细节的步骤做就天青门户如follwoing:

  1. 检查集合中的文件的数目

enter image description here

  • 在集合中创建一个农产品商店
  • enter image description here

  • 检查所有的集合中的文件已被删除
  • enter image description here