Collection Support
Couchbase 支持 Scopes and Collections。本部分记录了如何将它与 Spring Data Couchbase 配合使用。
Couchbase supports Scopes and Collections. This section documents on how to use it with Spring Data Couchbase.
try-cb-spring 示例应用程序是对在 Spring Data Couchbase 中使用范围和集合的实际演示。
The try-cb-spring sample application is a working example of using Scopes and Collections in Spring Data Couchbase.
可以在 Presentation Only 和 Presentation with Slide Deck 中找到 2021 Couchbase Connect 关于 Spring Data 中集合的演示文稿
The 2021 Couchbase Connect presentation on Collections in Spring Data can be found at Presentation Only and Presentation with Slide Deck
Getting Started & Configuration
Scope and Collection Specification
有一些指定作用域和集合的机制,它们可以结合使用,或其中的一种机制覆盖另一种机制。首先是对作用域和集合进行一些定义。未指定的作用域表示使用默认的作用域,同样的,未指定的集合表示使用默认的集合。只有三种有效的作用域和集合的组合。(1)默认的作用域和默认的集合;(2)默认的作用域和非默认的集合;(3)非默认的作用域和非默认的集合。不可能有非默认的作用域和默认的集合,因为非默认的作用域不包含默认的集合,同样也不可能创建它。
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.
可以在配置中指定作用域:
A scope can be specified in the configuration:
@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:
@Document
@Scope("travel")
@Collection("airport")
public class Airport {...
@Scope("travel")
@Collection("airport")
public interface AirportRepository extends CouchbaseRepository<Airport, String> ...
作用域和集合可以使用 inScope(scopeName) 和 inCollection(collectionName) 流畅 API 指定在模板上:
Scopes and Collections can be specified on templates using the inScope(scopeName) and inCollection(collectionName) fluent APIs:
List<Airport> airports = template.findByQuery(Airport.class).inScope("archived").all()
作用域和集合可以使用 withScope(scopeName) 和 withCollection(collectionName) API 指定在扩展了 DynamicProxyable 的存储库上:
Scopes and Collections can be specified on repositories that extend DynamicProxyable using the withScope(scopeName) and withCollection(collectionName) APIs:
public interface AirportRepository extends CouchbaseRepository<Airport, String>, DynamicProxyable<AirportRepository>{...}
...
List<Airport> airports = airportRepository.withScope("archived").findByName(iata);
-
inScope()/inCollection() of the template fluent api
-
withScope()/withCollection() of the template/repository object
-
annotation of the repository method
-
annotation of the repository interface
-
annotation of the entity object
-
getScope() of the configuration