如何获取RavenDB文档的名称

时间:2012-04-25 07:14:28

标签: database linq ravendb

我的代码是:

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

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

但它没有给我文件的名称。我想在单个数据库中列出所有文档。

2 个答案:

答案 0 :(得分: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());
    }
}

}

答案 1 :(得分:0)

修改Matt waren的指定数据库代码。

 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());
                    }
                }
            }
        }