简体   繁体   中英

How can I get all entity names (collection names) from a RavenDB database?

I need retrieve all RavenDB collection names present into a database. Is there any way to do this? How?

If you've used the studio at all, you'll see it created an index called Raven/DocumentsByEntityName . If you don't use the studio, or you want to ensure it's there, you can build it yourself with:

documentStore.ExecuteIndex(new RavenDocumentsByEntityName());

The way that Raven Studio builds it's "collections" list is by getting the terms of the Tag field from this index. You can do the same thing:

var results = documentStore.DatabaseCommands
                           .GetTerms("Raven/DocumentsByEntityName", "Tag", "", 1024);

(If you expect more than 1024 different entity types for some reason, then paginate.)

Finally I got a solution by trying with my knowledges acquired learning documentation previously. I would want to advert people I only come here when I'm exausted with tentatives.

public string[] getCollectionNames()
        {
            using (var session = this.store.OpenSession())
            {
                return session.Advanced.LuceneQuery<dynamic>()
                           .SelectFields<dynamic>("@metadata")
                           .Select<dynamic, string>(x => x["@metadata"]["Raven-Entity-Name"])
                           .Distinct()
                           .ToArray();

            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM