In previous editions of Couchbase, we used the following steps to model data:
- Put everything into one bucket.
- To distinguish different types, add a
typefield to each JSON document and introduce a single index on
- 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 the
airportdocument type. Add index 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
production. For one application deployment, everything will then go to
- JSON documents do not need a
typefield. 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 for
typefield, since the
FROM clausewill specify
bucket.development.airportfor each query. Index on
typeis no longer needed.
- Lookup documents for a type get their own collection, like
airport-constraintsand 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 without
typeindex. 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
- 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.
- Are scopes really not useful to group collections together in the following way and answer questions like: "Give me all documents from
this = that"? In other words, when querying I noticed that it is not possible to specify
bucket.scope. When the scope is specified, then the collection also must be specified. Is that correct?