createCollection
On this page
Creates a new collection.createCollection
command creates a columnstore table.
Syntax
db.createCollection(<name>,{timeseries: {timeField: <string>},rowStore: <boolean>shardKey: <document>sortKey: <document>indexes: <array>columns: <array>preserveJSONKeyOrder: <boolean>from: <document>})
Option |
Type |
Description |
---|---|---|
|
String |
Creates a top-level |
|
Boolean |
When enabled, creates a rowstore table. |
|
Document |
Defines a shard key for the collection at creation in the index format. |
|
Document |
Defines a |
|
Array |
Each document in the array contains an index definition in the type specified in |
|
Array |
Each document in the array represents a top-level column that is created in the table supporting this collection, with an |
|
Boolean |
When enabled, key order is recorded by adding a field named |
|
Boolean |
When enabled, creates a column group index on the collection. Note: Column group indexes are only supported on columnstore tables. |
|
Document |
Specifies an external collection to synchronize data with, in the following format:
|
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.
Enable the uniqueUnenforced
option to enable unenforced unique keys across the database as follows:
use <database>db.runCommand({setDefaultCollectionOptions: 1, value: {uniqueUnenforced: true}})
Collections created after this option is enabled do not enforce the unique
constraint, i.
For example,
use dbTest;db.createCollection("records",{ rowStore: false });// Create an index with a unique key, unsupporteddb.records.createIndex({ unique_id: 1 }, { unique: true });
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:
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:
db.createCollection("exampleC", {columns: [{ id: "Code", type: "BIGINT NOT NULL" }],}); -
Create a collection supported by a rowstore table:
db.createCollection("exCollection", { rowstore: true }); -
Replicate a collection dbTest.
exampleC from a remote MongoDB® server defined using a link named lnkExample: use dbTest;db.createCollection("exampleC", { from: { link: "lnkExample" } });Refer to Replicate MongoDB® Collections to SingleStore for more information.
-
Create a collection with column group enabled:
db.createCollection("exampleCollection", { columnGroup: true });
Last modified: June 4, 2025