2012-04-25 92 views
2

我的代碼是:如何獲得RavenDB文件的名稱

using (var session = documentStore.OpenSession(databaseName)) 
{ 
    var list = session.Query<dynamic>("Raven/DocumentsByEntityName").ToArray(); 

    foreach (var item in list) 
    { 
     Console.WriteLine(item); 
    } 
} 

但它並沒有給我的文檔的名稱。我想列出單個數據庫中的所有文檔。

+0

你想做什麼,你爲什麼要列出數據庫中的所有文檔? – 2012-04-25 13:25:27

+0

如果您只想查看所有文檔,請啓動ravendb服務器並訪問http:// localhost:8080/docs或http:// localhost:8080/raven/documents.html – 2012-04-25 13:27:41

+0

您好Matt,我正在構建GUI Ravendb和我的操作之一是檢索單個數據庫中的所有文檔。可能嗎 ? – 2012-04-26 05:45:55

回答

2

嘗試這樣的事情,這是一個有點更通用的,它允許訪問原始文件

using (var session = store.OpenSession()) 
{ 
    //Issue a dummy query to make sure the indexing has finished 
    var dummyQuery = session.Query<dynamic>("Raven/DocumentsByEntityName") 
     .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) 
     .ToList(); 

    //First get all the document types, i.e. the different entity names 
    var docTypes = store.DatabaseCommands.GetTerms("Raven/DocumentsByEntityName", "Tag", "", 128); 
    foreach (var type in docTypes) 
    { 
     Console.WriteLine("\n{0}:", type); 
     //Might need to do paging here, can only get at most 1024 docs in 1 go! 
     var docs = store.DatabaseCommands.StartsWith(type, 0, 1024).ToList(); 

    foreach (var doc in docs) 
    { 
     Console.WriteLine(" {0}: {1}", doc.Key, doc.ToJson()); 
    } 
} 

}

+0

非常感謝馬特。真的行。它正在爲默認數據庫工作。我修改了指定數據庫的代碼併發布它。:) – 2012-04-27 05:02:16

+0

store.DatabaseCommands.GetTerms()中字符串「tag」的用法是什麼? – 2012-04-27 07:10:42

+0

這是您希望獲得的術語的名稱,「Raven/DocumentsByEntityName」使用的術語的名稱,請參閱https://github.com/ravendb/ravendb/blob/master/Raven.Database/Plugins/Builtins/ CreateSilverlightIndexes.cs#L17 – 2012-04-27 08:09:01

0

修改馬特瓦倫的代碼指定的數據庫。

public void DocumentNamesWithMetadata(string databaseName="1") 
     { 
      using (var session = documentStore.OpenSession(databaseName)) 
      { 
       //Issue a dummy query to make sure the indexing has finished 
       var dummyQuery = session.Query<dynamic>("Raven/DocumentsByEntityName") 
             .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) 
             .ToList(); 

       //First get all the document types, i.e. the different entity names 
       var docTypes = session.Advanced.DatabaseCommands.GetTerms("Raven/DocumentsByEntityName", "Tag", "", 128); 
       foreach (var type in docTypes) 
       { 
        Console.WriteLine("\n{0}:", type); 
        //Might need to do paging here, can only get at most 1024 docs in 1 go! 
        var docs = session.Advanced.DatabaseCommands.StartsWith(type, 0, 1024).ToList(); 

        foreach (var doc in docs) 
        { 
         Console.WriteLine(" {0}: {1}", doc.Key, doc.ToJson()); 
        } 
       } 
      } 
     }