Raven DB: How can I delete all documents of a given type

后端 未结 4 890
旧巷少年郎
旧巷少年郎 2021-02-04 04:52

More specifically in Raven DB, I want to create a generic method with a signature like;

public void Clear() {...

Then have Raven DB cl

4条回答
  •  自闭症患者
    2021-02-04 05:19

    I assume you want to do this from the .NET client. If so, use the standard DocumentsByEntityName index:

    var indexQuery = new IndexQuery { Query = "Tag:" + collectionName };
    session.Advanced.DocumentStore.DatabaseCommands.DeleteByIndex(
       "Raven/DocumentsByEntityName", 
       indexQuery, 
       new BulkOperationOptions { AllowStale = true });
    
    var hilo = session.Advanced.DocumentStore.DatabaseCommands.Get("Raven/H‌​ilo/", collectionName);
    if (hilo != null) {
        session.Advanced.DocumentStore.DatabaseCommands.Delete(hilo.‌​Key, hilo.Etag);
    }
    

    Where collectionName is the actual name of your collection.

    The first operation deletes the items. The second deletes the HiLo file.

    Also check out the official documentation - How to delete or update documents using index.

提交回复
热议问题