Documentation Home
X DevAPI User Guide
Download this Manual
PDF (US Ltr) - 1.4Mb
PDF (A4) - 1.3Mb


X DevAPI User Guide  /  ...  /  Creating a Collection

4.2.1 Creating a Collection

In order to create a new collection call the createCollection() function from a Schema object. It returns a Collection object that can be used right away to, for example, insert documents into the database.

Optionally, for Connectors, the field reuseExistingObject can be set to true and passed as second parameter to prevent an error being generated if a collection with the same name already exists.

MySQL Shell JavaScript Code

// Create a new collection called 'my_collection'
var myColl = db.createCollection('my_collection');

MySQL Shell Python Code

# Create a new collection called 'my_collection'
myColl = db.create_collection('my_collection')

Node.js JavaScript Code

// Create a new collection called 'my_collection'
var promise = db.createCollection('my_collection');

// Create a new collection or reuse existing one
var promise = db.createCollection('my_collection', { ReuseExistingObject: true } );

C# Code

// Create a new collection called "my_collection"
var myColl = db.CreateCollection("my_collection");

// Create a new collection or reuse existing one
var myExistingColl = db.CreateCollection("my_collection", ReuseExistingObject: true);

Python Code

# Create a new collection called 'my_collection'
my_coll = my_schema.create_collection('my_collection')
# Create a new collection or reuse existing one
my_coll = my_schema.create_collection('my_collection', true)

Java Code

// Create a new collection called 'my_collection'
Collection myColl = db.createCollection("my_collection");

// Create a new collection or reuse existing one
// Second parameter is: boolean reuseExistingObject
Collection myExistingColl = db.createCollection("my_collection", true);

C++ Code

// Create a new collection called 'my_collection'
Collection myColl = db.createCollection("my_collection");

// Create a new collection or reuse existing one
Collection myExistingColl = db.createCollection("my_collection", true);