How to setup a default database?

DefaultDb parameter in DocumentStore constructor allows you to setup a default database for a DocumentStore. Implication of setting up a default database is that each time you access Commands or create a Session without explicitly passing database on which they should operate on then default database is assumed.

Example I

// without specifying `DefaultDb`
// created `DatabaseCommands` or opened `Sessions`
// will work on `<system>` database by default
// if no database is passed explicitly
try (IDocumentStore store = new DocumentStore("http://localhost:8080").initialize()) {
  IDatabaseCommands commands = store.getDatabaseCommands();
  try (IDocumentSession session = store.openSession()) {
    // ...
  }

  IDatabaseCommands northwindCommands = commands.forDatabase("Northwind");
  try (IDocumentSession northwindSession =  store.openSession("Northwind")) {
    // ...
  }
}

Example II

// when `DefaultDatabase` is set to `Northwind`
// created `DatabaseCommands` or opened `Sessions`
// will work on `Northwind` database by default
// if no database is passed explicitly
try (IDocumentStore store = new DocumentStore("http://localhost:8080", "Northwind").initialize()) {
  IDatabaseCommands northwindCommands = store.getDatabaseCommands();
  try (IDocumentSession northwindSession = store.openSession()) {
    // ...
  }

  IDatabaseCommands adventureWorksCommands = northwindCommands.forDatabase("AdventureWorks");
  try (IDocumentSession adventureWorksSession = store.openSession("AdventureWorks")) {
    // ...
  }

  IDatabaseCommands systemCommands = northwindCommands.forSystemDatabase();
  try (IDocumentSession systemSession = store.openSession(Constants.SYSTEM_DATABASE)) {
    // ...
  }
}

Remarks

Note

By default value of DefaultDb property in DocumentStore is null which means that actions will be executed on <system> database.