Client API: How to Setup a Default Database

database property allows you to setup a default database for a DocumentStore. Implication of setting up a default database is that each time you access operations or create a session without explicitly passing database on which they should operate on then default database is assumed.

Example I

// without specifying `database`
// we will need to specify the database in each action
// if no database is passed explicitly we will get an exception

try (DocumentStore store = new DocumentStore()) {
    store.setUrls(new String[]{ "http://localhost:8080" });

    try (IDocumentSession session = store.openSession("Northwind")) {
        // ...

    CompactSettings compactSettings = new CompactSettings();
    store.maintenance().server().send(new CompactDatabaseOperation(compactSettings));

Example II

// when `database` is set to `Northwind`
// created `operations` or opened `sessions`
// will work on `Northwind` database by default
// if no database is passed explicitly
try (DocumentStore store = new DocumentStore(new String[]{ "http://localhost:8080" }, "Northwind")) {

    try (IDocumentSession northwindSession = store.openSession()) {
        // ...

    store.maintenance().send(new DeleteIndexOperation("NorthwindIndex"));

    try (IDocumentSession adventureWorksSession = store.openSession("AdventureWorks")) {
        // ...

    store.maintenance().forDatabase("AdventureWorks").send(new DeleteIndexOperation("AdventureWorksIndex"));



By default value of database property in DocumentStore is null which means that in any actions that need a database name we will have to specify the database.