# createCollection

Creates a new collection. SingleStore Kai creates a collection implicitly on write. By default, the `createCollection` command creates a columnstore table.

## Syntax

```mongodb
db.createCollection(<name>,
   {
     timeseries: {
        timeField: <string>
     },
     rowStore: <boolean>
     shardKey: <document>
     sortKey: <document>
     indexes: <array>
     columns: <array>
     preserveJSONKeyOrder: <boolean>
     from: <document>
   }
)
```

| Option                 | Type     | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ---------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `timeseries.timeField` | String   | Creates a top-level`DATETIME(6)`field and marks it with`SERIES TIMESTAMP`. Additionally, it creates a`SORT KEY`on this field, which accelerates time-related queries.                                                                                                                                                                                                                                                                                                                                                                           |
| `rowStore`             | Boolean  | When enabled, creates a rowstore table.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `shardKey`             | Document | Defines a shard key for the collection at creation in the index format. For example,`{a:1}`.                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `sortKey`              | Document | Defines a`SORT KEY`for the collection in the index format. For example,`{a:1}`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `indexes`              | Array    | Each document in the array contains an index definition in the type specified in`createIndex`. For example,`{name:"indexExample", key:{a:1}}`.                                                                                                                                                                                                                                                                                                                                                                                                  |
| `columns`              | Array    | Each document in the array represents a top-level column that is created in the table supporting this collection, with an`id`and a`type`. If the`type`is not specified, it is assumed to be JSON by default. For example:`{id:"mycolumn",type:"BIGINT NOT NULL"}`.                                                                                                                                                                                                                                                                              |
| `preserveJSONKeyOrder` | Boolean  | When enabled, key order is recorded by adding a field named`$$`into each level of the documents. This key order is retrieved when the document is read through the API. By default,SingleStorealphabetizes the JSON key order.                                                                                                                                                                                                                                                                                                                  |
| `columnGroup`          | Boolean  | When enabled, creates a column group index on the collection. Column group indexes improve the performance of queries where the entire document is projected. Refer to[How the Columnstore Works](https://docs.singlestore.com/cloud/create-a-database/columnstore/how-the-columnstore-works.md)for more information.**Note**: Column group indexes are only supported on columnstore tables.                                                                                                                                                   |
| `from`                 | Document | Specifies an external collection to synchronize data with, in the following format:`{   link: <string>   database: <string>   collection: <string> }`<ul> <li><code>link</code> (Required): Specifies the name of the link created using the <code>createLink</code> command.</li> <li><code>database</code> (Optional): Name of the source database. If not specified, the current database is used.</li> <li><code>collection</code>: (Optional): Name of the source collection. If not specified, the current collection is used.</li> </ul> |

The following options are not supported in a `createCollection` statement:

* `capped`
* `timeseries.metaField`
* `timeseries.granularity`
* `clusteredIndex`
* `changeStreamPreAndPostImages
  `
* `size`
* `max`
* `storageEngine`
* `validator`
* `validationLevel`
* `validationAction`
* `indexOptionDefaults`
* `viewOn`
* `pipeline
  `
* `collation`
* `writeConcern`

## Unenforced Unique Keys

SingleStore allows you to create multiple "unenforced" unique indexes on collections created using the Kai API. Unenforced unique keys enable applications that create unique or unsupported indexes on collections to run on SingleStore without any code changes and enhance compatibility with existing MongoDB® applications.

Enable the `uniqueUnenforced` option to enable unenforced unique keys across the database as follows:

```MongoDB
use <database>
db.runCommand({setDefaultCollectionOptions: 1, value: {uniqueUnenforced: true}})
```

Collections created after this option is enabled do not enforce the `unique` constraint, i.e. SingleStore does not block creation of multiple unique or unsupported indexes on a collection. Note that existing collections, created before this option is enabled, are not affected.

For example,

```MongoDB
use dbTest;
db.createCollection("records",
   { rowStore: false }
);

// Create an index with a unique key, unsupported
db.records.createIndex(
  { unique_id: 1 }, { unique: true }
);

```

```output

The unique key named: 'unique_id_1($unique_id)' cannot be created because unique keys must contain all columns of the shard key '($_id)'.
```

The `createIndex` command returns an error as shown in the output.

Now enable the `uniqueUnenforced` option, drop the `records` collection, and run the same commands again:

```MongoDB
db.runCommand({setDefaultCollectionOptions: 1, value: {uniqueUnenforced: true}});

db.records.drop();

db.createCollection("records",
   { rowStore: false }
);

db.records.createIndex(
  { unique_id: 1 }, { unique: true }
);
```

Because unique indexes are now unenforced, this command runs successfully.

## Examples

The following examples show how to use the `createCollection` command:

* Create a collection named **exampleC** with a field named **Code**:
  ```mongodb
  db.createCollection("exampleC", {
    columns: [{ id: "Code", type: "BIGINT NOT NULL" }],
  });
  ```
* Create a collection supported by a rowstore table:
  ```mongodb
  db.createCollection("exCollection", { rowstore: true });
  ```
* Replicate a collection **dbTest.exampleC** from a remote MongoDB® server defined using a link named **lnkExample**:
  ```MongoDB
  use dbTest;
  db.createCollection("exampleC", { from: { link: "lnkExample" } });
  ```
  Refer to [Replicate MongoDB® Collections to SingleStore](https://docs.singlestore.com/cloud/reference/singlestore-kai/replicate-mongodb-collections-to-singlestore.md) for more information.
* Create a collection with column group enabled:
  ```MongoDB
  db.createCollection("exampleCollection", { columnGroup: true });
  ```

***

Modified at: October 1, 2025

Source: [/cloud/reference/singlestore-kai/singlestore-extension-commands/createcollection/](https://docs.singlestore.com/cloud/reference/singlestore-kai/singlestore-extension-commands/createcollection/)

(An index of the documentation is available at /llms.txt)
