Skip to main content

MongoDB Database Actions

A Database Action in Ductape defines a reusable MongoDB operation (such as create, read, update, or delete) that can be performed on your product's MongoDB database. Database actions let you centralize and manage your database logic, making it easy to reuse, update, and secure queries across your application.

Note: Database Action tags should follow the format: databaseTag:dbActionTag. This ensures clarity and prevents conflicts across different databases and their actions.

Supported Action Types

Action TypeMongoDB OperationDescription
Readfind, findOneRetrieve documents from the collection
CountcountDocuments, estimatedDocumentCountCount documents in the collection
CreateinsertOne, insertManyAdd new documents to the collection
UpdateupdateOne, updateManyModify existing documents
DeletedeleteOne, deleteManyRemove documents from the collection
AggregateaggregatePerform aggregation pipelines

Data Validation

You can define data validation for each input using the following pattern:

'{{key:type:minlength:maxlength:unique}}'
  • key: The input field name (required)
  • type: Data type (optional, default: string)
  • minlength: Minimum length/size (optional, default: 1)
  • maxlength: Maximum length/size (optional, default: unlimited)
  • unique: Whether the value must be unique (optional, default: false)

Example:

'{{username:STRING:3:20:true}}' // username must be a unique string, 3-20 characters
'{{age:INTEGER:1:3}}' // age must be an integer, 1-3 digits
'{{email:EMAIL_STRING}}' // email must be a valid email string

Data validation patterns are used in your action templates to ensure inputs meet your requirements before executing the operation.

Available Data Types

TypeDescription
STRINGFree-form text
NOSPACES_STRINGString without spaces
EMAIL_STRINGString in a valid email format
DATE_STRINGString in a valid date format
NUMBER_STRINGString representing a number
INTEGERInteger value
DATEDate value
FLOATFloating-point number
DOUBLEDouble-precision floating-point number
UUIDUniversally Unique Identifier (UUID)
ARRAYArray of items
OBJECTJSON object
BOOLEANBoolean value (true or false)

Creating MongoDB Database Actions

Below are examples of how to create different types of actions. Each action object should include all required fields:

  • name: Human-readable name for the action
  • tag: Unique identifier for the action (use databaseTag:dbActionTag format)
  • tableName: Name of the MongoDB collection
  • type: Action type (see above)
  • template: MongoDB operation template (see below)
  • description: (Optional) Description of the action

Create Operation (insertOne)

import { IProductDatabaseAction, DatabaseActionTypes, DataTypes } from '@ductape/sdk/types';

const data: IProductDatabaseAction = {
name: 'Create User',
tag: 'mongo-db-tag:create-user',
tableName: 'users',
type: DatabaseActionTypes.CREATE,
template: {
insertOne: {
username: `{{username:${DataTypes.NOSPACES_STRING}:3:20:true}}`,
firstname: `{{firstname:${DataTypes.STRING}}}`,
lastname: `{{lastname:${DataTypes.STRING}}}`,
dateOfBirth: `{{dateOfBirth:${DataTypes.DATE_STRING}}}`,
address: `{{address:${DataTypes.STRING}}}`,
occupation: `{{occupation:${DataTypes.STRING}}}`
}
},
description: 'Create a new user in MongoDB'
};

const action = await ductape.product.databases.actions.create(data);

Create Operation (insertMany)

import { IProductDatabaseAction, DatabaseActionTypes, DataTypes } from '@ductape/sdk/types';

const data: IProductDatabaseAction = {
name: 'Create Multiple Users',
tag: 'mongo-db-tag:create-users',
tableName: 'users',
type: DatabaseActionTypes.CREATE,
template: {
insertMany: [
{
username: `{{username:${DataTypes.NOSPACES_STRING}:3:20:true}}`,
firstname: `{{firstname:${DataTypes.STRING}}}`,
lastname: `{{lastname:${DataTypes.STRING}}}`,
dateOfBirth: `{{dateOfBirth:${DataTypes.DATE_STRING}}}`,
address: `{{address:${DataTypes.STRING}}}`,
occupation: `{{occupation:${DataTypes.STRING}}}`
}
]
},
description: 'Create multiple users in MongoDB'
};

const action = await ductape.product.databases.actions.create(data);

Read Operation (findOne)

import { IProductDatabaseAction, DatabaseActionTypes } from '@ductape/sdk/types';

const data: IProductDatabaseAction = {
name: 'Read User',
tag: 'mongo-db-tag:read-user',
tableName: 'users',
type: DatabaseActionTypes.READ,
template: {
findOne: {
username: '{{username}}'
}
},
description: 'Read a user from MongoDB'
};

const action = await ductape.product.databases.actions.create(data);

Update Operation (updateOne)

import { IProductDatabaseAction, DatabaseActionTypes, DataTypes } from '@ductape/sdk/types';

const data: IProductDatabaseAction = {
name: 'Update User',
tag: 'mongo-db-tag:update-user',
tableName: 'users',
type: DatabaseActionTypes.UPDATE,
template: {
updateOne: {
set: {
firstname: `{{firstname:${DataTypes.STRING}}}`,
lastname: `{{lastname:${DataTypes.STRING}}}`,
address: `{{address:${DataTypes.STRING}}}`
}
}
},
filterTemplate: {
where: {
username: '{{username}}'
}
},
description: 'Update a user in MongoDB'
};

const action = await ductape.product.databases.actions.create(data);

Delete Operation (deleteOne)

import { IProductDatabaseAction, DatabaseActionTypes } from '@ductape/sdk/types';

const data: IProductDatabaseAction = {
name: 'Delete User',
tag: 'mongo-db-tag:delete-user',
tableName: 'users',
type: DatabaseActionTypes.DELETE,
template: {
deleteOne: {
username: '{{username}}'
}
},
description: 'Delete a user from MongoDB'
};

const action = await ductape.product.databases.actions.create(data);

Updating and Fetching Actions


Tip: Always ensure your template matches the syntax and requirements of your target database. Use parameter placeholders (e.g., {{username}}) to safely inject user input.

Next Steps