Skip to main content

Create Collection

Creates a new collection within a MongoDB database.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.

Inputs

  • MongoDB Client Id - The client ID returned from the Connect node (optional if credentials are provided).
  • Database Name - The name of the database where the collection will be created.
  • Collection Name - The name of the collection to create.

Options

  • Credentials - Database credentials (Category 5) - optional if using Client ID from Connect node. This allows you to perform the operation without a separate Connect node.

How It Works

The Create Collection node explicitly creates a new collection in a MongoDB database. When executed, the node:

  1. Validates that both database name and collection name are not empty
  2. Obtains a MongoDB client (either from client ID or by creating one from credentials)
  3. Accesses the specified database
  4. Calls the CreateCollection command with the specified collection name
  5. The new collection is created and becomes immediately available for use

Requirements

  • Either a valid client ID from Connect node OR database credentials
  • Valid database name (non-empty) - the database must already exist
  • Valid collection name (non-empty)
  • Appropriate permissions to create collections in the database

Error Handling

The node will return specific errors in the following cases:

  • ErrInvalidArg - Database name or collection name is empty, or client/credentials are invalid
  • ErrConnection - Cannot connect to MongoDB (when using credentials)
  • Database not found error if the specified database doesn't exist
  • Collection already exists error (if the collection name is already in use)
  • Permission errors if the user doesn't have collection creation rights

Usage Notes

  • MongoDB also creates collections automatically when you first insert data
  • Use this node when you need to explicitly create a collection before inserting data
  • Collection names are case-sensitive
  • Collections in the same database must have unique names
  • You can use either the client ID approach or direct credentials approach
  • The database must exist before creating a collection in it
  • Special characters and spaces in collection names should be avoided

Collection Naming Best Practices

  • Use lowercase names for consistency
  • Use plural nouns (e.g., "users", "products", "orders")
  • Use underscores to separate words (e.g., "customer_orders")
  • Keep names descriptive but concise
  • Avoid names starting with "system." (reserved for system collections)
  • Don't use the dollar sign ($) or null character in collection names

Example Usage

Scenario 1: Create collection using Connect node

  1. Connect node → Client Id
  2. Create Collection:
    • MongoDB Client Id: (from Connect)
    • Database Name: "ecommerce"
    • Collection Name: "products"

Scenario 2: Create collection using direct credentials

  1. Create Collection:
    • MongoDB Client Id: (leave empty)
    • Database Name: "inventory"
    • Collection Name: "items"
    • Credentials: (select database credential)

Scenario 3: Create multiple collections for a new database

Connect → Client Id

Create Database:
- Database Name: "customer_portal"

Create Collection:
- Database Name: "customer_portal"
- Collection Name: "users"

Create Collection:
- Database Name: "customer_portal"
- Collection Name: "sessions"

Create Collection:
- Database Name: "customer_portal"
- Collection Name: "audit_logs"

Disconnect

Scenario 4: Conditional collection creation

Show Collections:
- Database Name: "mydb"
- Output: collections

Set Variable:
- collection_exists = collections.includes("orders")

If NOT collection_exists:
Create Collection:
- Database Name: "mydb"
- Collection Name: "orders"

Common Use Cases

  • Schema Design: Explicitly create collections as part of database schema setup
  • Data Organization: Create separate collections for different data types
  • Multi-tenancy: Create collections for different customers or tenants
  • Migration: Set up collection structure before data import
  • Testing: Create clean collections for test data
  • Performance: Pre-create collections with specific configurations

Common Errors

Empty Database Name:

  • Cause: Database name input is empty or not provided
  • Solution: Provide a valid, non-empty database name

Empty Collection Name:

  • Cause: Collection name input is empty or not provided
  • Solution: Provide a valid, non-empty collection name

Database Does Not Exist:

  • Cause: Attempting to create a collection in a non-existent database
  • Solution: Create the database first using Create Database node

Collection Already Exists:

  • Cause: A collection with the same name already exists in the database
  • Solution: Use a different collection name or drop the existing collection first

Invalid Collection Name:

  • Cause: Collection name contains prohibited characters
  • Solution: Use only alphanumeric characters, underscores, and hyphens

Permission Denied:

  • Cause: User doesn't have permission to create collections
  • Solution: Ensure the database user has appropriate privileges (readWrite or dbAdmin role)
  • Drop Collection - Delete a collection
  • Show Collections - List all collections in a database
  • Insert Document - Add documents to a collection
  • Create Database - Create the parent database