Delete RavenDB collection
I need to delete a whole collection of documents in Raven DB. Deleting one by one (documents) is not a wise choice. 开发者_如何学编程Is there a way I can do this easily?
You can do a set based operation.
store.DatabaseCommands.DeleteByIndex() to do so
store.DatabaseCommands.DeleteByIndex(
"Enquiries/MyEnquiryIndexName",
new IndexQuery { Query = "Id:*", },
allowStale: false);
Code sample by @Marijin
Not sure about previous versions, but below applies to RavenDB 5.0
If you wanted to delete all documents from a collection called "Users", you could pass in the collection name to the DeleteByQueryOperation
DeleteByQueryOperation("from Users")
A generic version would look something like this:
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}"));
}
}
精彩评论