In previous editions of Couchbase, we used the following steps to model data:
- Put everything into one bucket.
- To distinguish different types, add a
type
field to each JSON document and introduce a single index onbucket.(type)
. - To ensure the uniqueness of certain fields for certain types, use lookup documents. To be able to query lookup documents, add
type = "constraint", for = "airport"
fields to lookup documents, meaning: the document is a unique constraint for theairport
document type. Add index forbucket.(for)
. - Add any other indexes into bucket level.
- Introduce a single primary index for bucket.
With Couchbase 7.0 and collections, we have to follow in mind:
- Put everything into one bucket and one specific scope per deployment environment, like
development
,staging
,production
. For one application deployment, everything will then go tobucket.development
. - JSON documents do not need a
type
field. Instead, the application checks adaptively whether a collection for that type exists, and if not, it will create the collection. It then uses that collection API from the SDK for K/V operations. Queries no longer check fortype
field, since theFROM clause
will specifybucket.development.airport
for each query. Index ontype
is no longer needed. - Lookup documents for a type get their own collection, like
airport-constraints
and lookup documents will be placed there using transactions, ensuring atomicity when an airport is inserted into the database with unique constraints. Constraints are now easily queried withouttype
index. This also means that for each type, we have 2 collections created: one stores the documents the other stores unique constraints. - Add any other indexes into collection level, like
bucket.development.airport
. - Introduce a primary index for each collection instead of bucket.
I would like to ask the community whether any of the above changes are anti-patterns or things that could be designed better for performance.
Additional questions:
- Are scopes really not useful to group collections together in the following way and answer questions like: "Give me all documents from
bucket.scope
wherethis = that
"? In other words, when querying I noticed that it is not possible to specifybucket.scope
. When the scope is specified, then the collection also must be specified. Is that correct?
Thanks!