A newer version of this documentation is available.

View Latest

Working with the Collections Developer Preview

    Collections is introduced as a Developer Preview feature in Couchbase Server 6.5. The 3.0 API SDKs all work with Collections and Scopes. As a practical demonstration, we have a collections-enabled version of the Travel Sample application.

    The Developer Preview of the upcoming Collections feature in Couchbase Server is fully implemented in the 3.0 API versions of the Couchbase SDKs. When working with other server versions, the defaultcollection is used from the SDK. Here we show how to access individual collections in the Developer Preview version of Couchbase Server 6.5, with a collections-enabled version of our Travel Sample application. User documents and flight documents are split into user and flight collections. Something that previously had to be done with key-value references to different types or categories of data.

    Travel Sample app — with Collections

    Travel Sample Application uses the Travel Sample data Bucket, which ships with Couchbase Server. For Couchbase Server 6.5, make sure that you have at least one node each of data; query; index; and search. For a development box, mixing more than one of these on a single node (given enough memory resources) is perfectly acceptable. If you have yet to install Couchbase Server in your development environment, start here.

    Then load up the Travel Sample Bucket, using either the Web interface or the command line. You will also need to create a Search Index — Query indexes are taken care of by the Sample Bucket.

    Enabling Developer Preview should only be done on a development machine; there is no upgrade path available from a DP-enabled Couchbase Server.


    As well as the Node.js SDK 3.0 and Couchbase Server, set up as described above, you will need git to fetch the travel sample application code:

    $ git clone https://github.com/couchbaselabs/try-cb-nodejs.git

    Change directory into your cloned repository, and check out the Collections branch (in the case of the Node.js SDK, 6.5-collections).

    $ git checkout 6.5-collections
    • Before building the Collections-enabled version of the Travel Sample Application, you need to enable this DP feature (see warning above).

      Enable Collections Developer Preview
      $ /opt/couchbase/bin/couchbase-cli enable-developer-preview --enable -c http://localhost:8091 -u Administrator -p password
      Developer preview cannot be disabled once it is enabled. If you enter developer preview mode you will not be able to upgrade. DO NOT USE IN PRODUCTION.
      Are you sure [y/n]: y

    The Travel Sample Bucket needs altering to be split into collections. There is a script to do this included with the Travel Sample App — run:

    Create Sample Collections
    $ sh create-collections.sh Administrator password

    adjusting for any changes you have made to server URL, or admin password. You should now have the Travel Sample Data Bucket split into collections:


    Running the Travel Sample Application

    Next, edit the storage.host field in src/main/resources/application.properties to the one for your containerised Couchbase Server (or localhost,, if appropriate), and any other local changes — such as password. From here onwards, we’ll assume the defaults.

    And run with

    $ npm run start

    After the build, with your Web browser of choice, head to port 8080 of the local machine — http://localhost:8080.

    Using the Sample App is the same as with the non-collections version that we cover in our introductory doc, but we’re assuming you’ve come here to see Collections in use in the codebase.

    Sample App Backend

    The backend code shows Couchbase Node.js SDK in action with Query and Search, but also how to plug together all of the elements and build an application with Couchbase Server and the Node.js SDK.

    Collections and Scope are set immediately after opening the bucket:

    try {
        const sampleOptions = { username: 'Administrator', password: 'password' };
        const sampleCluster = new couchbase.Cluster("localhost", sampleOptions);
        const sampleBucket = sampleCluster.bucket("travel-sample");
        const sampleScope = sampleBucket.scope("tenant_agent_00");
        sampleColl = sampleScope.collection("users");
    } catch (e) {
        console.log("travel-sample bucket, scope or collection not set up?");

    And operations are now performed on the specific collection, rather than the whole bucket (or the whole bucket via _default collection, as in Server 6.5 without the Developer Preview enabled).

    result = await sampleColl.get(user);