For the latest stable version, please use Spring Data Couchbase 5.4.0!spring-doc.cn

Collection Support

Couchbase supports Scopes and Collections. This section documents on how to use it with Spring Data Couchbase.spring-doc.cn

The try-cb-spring sample application is a working example of using Scopes and Collections in Spring Data Couchbase.spring-doc.cn

The 2021 Couchbase Connect presentation on Collections in Spring Data can be found at Presentation Only and Presentation with Slide Deckspring-doc.cn

Requirements

Getting Started & Configuration

Scope and Collection Specification

There are several mechanisms of specifying scopes and collections, and these may be combined, or one mechanism may override another. First some definitions for scopes and collections. An unspecified scope indicates that the default scope is to be used, likewise, an unspecified collection indicates that the default collection is to be used. There are only three combinations of scopes and collections that are valid. (1) the default scope and the default collection; (2) the default scope and a non-default collection; and (3) a non-default scope and a non-default collection. It is not possible to have a non-default scope and a default collection as non-default scopes do not contain a default collections, neither can one be created.spring-doc.cn

A scope can be specified in the configuration:spring-doc.cn

@Configuration
static class Config extends AbstractCouchbaseConfiguration {

    // Usual Setup
    @Override public String getConnectionString() { /* ... */ }

    // optionally specify the scope in the Configuration
    @Override
    protected String getScopeName() {
        return "myScope"; // or a variable etc.;
    }

}

Scopes and Collections can be specified as annotations on entity classes and repositories:spring-doc.cn

@Document
@Scope("travel")
@Collection("airport")
public class Airport {...
@Scope("travel")
@Collection("airport")
public interface AirportRepository extends CouchbaseRepository<Airport, String> ...

Scopes and Collections can be specified on templates using the inScope(scopeName) and inCollection(collectionName) fluent APIs:spring-doc.cn

List<Airport> airports = template.findByQuery(Airport.class).inScope("archived").all()

Scopes and Collections can be specified on repositories that extend DynamicProxyable using the withScope(scopeName) and withCollection(collectionName) APIs:spring-doc.cn

public interface AirportRepository extends CouchbaseRepository<Airport, String>, DynamicProxyable<AirportRepository>{...}
...
List<Airport> airports = airportRepository.withScope("archived").findByName(iata);
The order of precedence is:
  1. inScope()/inCollection() of the template fluent apispring-doc.cn

  2. withScope()/withCollection() of the template/repository objectspring-doc.cn

  3. annotation of the repository methodspring-doc.cn

  4. annotation of the repository interfacespring-doc.cn

  5. annotation of the entity objectspring-doc.cn

  6. getScope() of the configurationspring-doc.cn