X DevAPI User Guide

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, for example to insert documents into the collection.

Optionally, 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')

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);