10

我需要删除 Raven DB 中的整个文档集合。逐一删除(文档)不是明智的选择。有没有办法可以轻松做到这一点?

4

2 回答 2

6

您可以执行基于集合的操作。

store.DatabaseCommands.DeleteByIndex()这样做

store.DatabaseCommands.DeleteByIndex(
    "Enquiries/MyEnquiryIndexName",
    new IndexQuery { Query = "Id:*", },
    allowStale: false);

@Marijin 的代码示例

于 2011-08-11T21:33:20.103 回答
0

不确定以前的版本,但以下适用于 RavenDB 5.0

如果您想从名为“Users”的集合中删除所有文档,您可以将集合名称传递给DeleteByQueryOperation

DeleteByQueryOperation("from Users")

通用版本看起来像这样:

using Raven.Client.Documents;
using Raven.Client.Documents.Operations;

public class ExampleClass
{
  public static void DeleteCollection<TEntity>(IDocumentStore store, string databaseName)
  {
    var collectionName = store.Conventions.GetCollectionName(typeof(TEntity));

    store.Operations
      .ForDatabase(databaseName)
      .Send(new DeleteByQueryOperation($"from {collectionName}"));
  }
}
于 2021-03-09T14:44:00.403 回答