You are viewing the documentation for a prerelease version.

CREATE COLLECTION

  • Couchbase Server 7.0
    +
    The CREATE COLLECTION statement enables you to create a named collection within a scope.

    Syntax

    create-collection ::= CREATE COLLECTION [ [ namespace ':' ] bucket '.' scope '.' ] collection
    'CREATE' 'COLLECTION' ( ( namespace ':' )? bucket '.' scope '.' )? collection
    namespace

    (Optional) An identifier that refers to the namespace of the bucket in which you want to create the collection. Currently, only the default namespace is available. If the namespace name is omitted, the default namespace in the current session is used.

    bucket

    (Optional) An identifier that refers to the bucket in which you want to create the collection.

    scope

    (Optional) An identifier that refers to the scope in which you want to create the collection.

    collection

    (Required) An identifier that refers to the name of the collection that you want to create. Refer to Naming for Scopes and Collections for restrictions on collection names.

    If there is a hyphen (-) inside the bucket name, the scope name, or the collection name, you must wrap that part of the path in backticks (` `). For example, default:`travel-sample` indicates the travel-sample keyspace in the default namespace.

    Usage

    To specify the collection, you may do one of the following:

    • Include its full path, containing the namespace, bucket, and scope, followed by the collection name;

    • Include a relative path, containing just the bucket and scope, followed by the connection name;

    • Specify just the collection name without a path.

    When you specify a collection name without a path, you must set the query context to indicate the required namespace, bucket, and scope. If you specify a collection name by itself without setting a valid query context, an error is generated.

    It is important to note that the scope must exist before you can create the collection, whether the scope is specified in the statement itself or implied by the query context. If the scope does not exist, an error is generated. You cannot create the scope and the collection in a single statement.

    Examples

    Example 1. Create collection with full path

    This statement creates a collection called cities in the places scope within the travel-sample bucket. It is assumed that the places scope already exists.

    CREATE SCOPE `travel-sample`.places.cities
    Example 2. Create collection with query context

    This statement creates a collection called countries in the places scope within the travel-sample bucket. It is assumed that the places scope already exists.

    This example uses the cbq shell, but you can also set the query context and create the collection using the Query Workbench.

    Set the query context
    cbq> \SET -query_context "travel-sample.places";
    Create the collection
    cbq> CREATE COLLECTION countries;