Skip to main content

MongoDB Database Actions

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

Action TypeMongoDB Operations
Readfind, findOne
CountcountDocuments, estimatedDocumentCount
CreateinsertOne, insertMany
UpdateupdateOne, updateMany
DeletedeleteOne, deleteMany
Aggregateaggregate

MongoDB Database Actions Creation

Please note that in MongoDB, the templates are defined as

  template: {
[MongoDB Opertation]: {
// template
}
}

Data Validation

To define data validation for each datapoint you can follow the pattern

'{{key:type:minlength:maxlength:unique}}'

All fields asides the key are optional

e.g

'{{key}}' is valid

When the non key values are not defined

The default values are

  • type - (string)
  • minlength - (1) one
  • maxlength - (0) unlimited
  • unique - (boolean) false

Available DataTypes Options

The following DataTypes are available for defining feature inputs:

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)

Create Operation

const data: IProductDatabaseAction = {
tag: 'mongo-db-tag:create-user',
tableName: 'users',
type: DatabaseActionTypes.CREATE,
template: {
insertOne: {
username: `{{username:${DataTypes.NOSPACES_STRING}:3:20}}`,
firstname: `{{firstname:${DataTypes.STRING}}}`,
lastname: `{{lastname:string:${DataTypes.STRING}}}`,
dateOfBirth: `{{dateOfBirth:date_string${DataTypes.DATE_STRING}}}`,
address: `{{address:string:${DataTypes.STRING}}}`,
occupation: `{{occupation:${DataTypes.STRING}}}`
}
}
};

The above shows an insertOne create action, you can also use create many with insertMany instead and expect an array of values like

const data: IProductDatabaseAction = {
tag: 'mongo-db-tag:create-users',
tableName: 'users',
type: DatabaseActionTypes.CREATE,
template: {
insertMany: [{
username: `{{username:${DataTypes.NOSPACES_STRING}:3:20}}`,
firstname: `{{firstname:${DataTypes.STRING}}}`,
lastname: `{{lastname:string:${DataTypes.STRING}}}`,
dateOfBirth: `{{dateOfBirth:date_string${DataTypes.DATE_STRING}}}`,
address: `{{address:string:${DataTypes.STRING}}}`,
occupation: `{{occupation:${DataTypes.STRING}}}`
}]
}
};

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

Read Operation

const data: IProductDatabaseAction = {
tag: 'mongo-db-tag:read-user',
tableName: 'users',
type: DatabaseActionTypes.READ,
template: {
findOne: {
username: '{{username}}'
}
}
};

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

Update Operation

const data: IProductDatabaseAction = {
tag: 'mongo-db-tag:update-user',
tableName: 'users',
type: DatabaseActionTypes.UPDATE,
template: {
updateOne: {
set: {
username: `{{username:${DataTypes.NOSPACES_STRING}:3:20}}`,
firstname: `{{firstname:${DataTypes.STRING}}}`,
lastname: `{{lastname:string:${DataTypes.STRING}}}`,
}
}
},
filterTemplate: {
where: {
username: '{{username}}'
}
}
};

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

Delete Operation

const data: IProductDatabaseAction = {
tag: 'mongo-db-tag:delete-user',
tableName: 'users',
type: DatabaseActionTypes.DELETE,
template: {
deleteOne: {
username: '{{username}}'
}
}
};

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