diff --git a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts index f11c3ce485..f939a1b9e2 100644 --- a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts +++ b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts @@ -7,6 +7,7 @@ import { INodeExecutionData, INodeType, INodeTypeDescription, + JsonObject, NodeOperationError } from 'n8n-workflow'; @@ -46,7 +47,33 @@ export class MongoDb implements INodeType { const items = this.getInputData(); const operation = this.getNodeParameter('operation', 0) as string; - if (operation === 'delete') { + if (operation === 'aggregate') { + // ---------------------------------- + // aggregate + // ---------------------------------- + + try { + const queryParameter = JSON.parse(this.getNodeParameter('query', 0) as string); + + if (queryParameter._id && typeof queryParameter._id === 'string') { + queryParameter._id = new ObjectID(queryParameter._id); + } + + const query = mdb + .collection(this.getNodeParameter('collection', 0) as string) + .aggregate(queryParameter); + + const queryResult = await query.toArray(); + + returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]); + } catch (error) { + if (this.continueOnFail()) { + returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message } ); + } else { + throw error; + } + } + } else if (operation === 'delete') { // ---------------------------------- // delete // ---------------------------------- @@ -59,7 +86,7 @@ export class MongoDb implements INodeType { returnItems = this.helpers.returnJsonArray([{ deletedCount }]); } catch (error) { if (this.continueOnFail()) { - returnItems = this.helpers.returnJsonArray({ error: error.message }); + returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message }); } else { throw error; } @@ -99,7 +126,7 @@ export class MongoDb implements INodeType { returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]); } catch (error) { if (this.continueOnFail()) { - returnItems = this.helpers.returnJsonArray({ error: error.message } ); + returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message } ); } else { throw error; } @@ -137,7 +164,7 @@ export class MongoDb implements INodeType { } } catch (error) { if (this.continueOnFail()) { - returnItems = this.helpers.returnJsonArray({ error: error.message }); + returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message }); } else { throw error; } @@ -188,7 +215,7 @@ export class MongoDb implements INodeType { .updateOne(filter, { $set: item }, updateOptions); } catch (error) { if (this.continueOnFail()) { - item.json = { error: error.message }; + item.json = { error: (error as JsonObject).message }; continue; } throw error; diff --git a/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts b/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts index c2bbadee18..6ed8f2854a 100644 --- a/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts +++ b/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts @@ -29,6 +29,11 @@ export const nodeDescription: INodeTypeDescription = { name: 'operation', type: 'options', options: [ + { + name: 'Aggregate', + value: 'aggregate', + description: 'Aggregate documents.', + }, { name: 'Delete', value: 'delete', @@ -63,6 +68,30 @@ export const nodeDescription: INodeTypeDescription = { description: 'MongoDB Collection', }, + // ---------------------------------- + // aggregate + // ---------------------------------- + { + displayName: 'Query', + name: 'query', + type: 'json', + typeOptions: { + alwaysOpenEditWindow: true, + }, + displayOptions: { + show: { + operation: [ + 'aggregate', + ], + }, + }, + default: '', + placeholder: `[{ "$match": { "$gt": "1950-01-01" }, ... }]`, + hint: 'Learn more about aggregation pipeline here', + required: true, + description: 'MongoDB aggregation pipeline query in JSON format', + }, + // ---------------------------------- // delete // ---------------------------------- @@ -149,6 +178,7 @@ export const nodeDescription: INodeTypeDescription = { required: true, description: 'MongoDB Find query.', }, + // ---------------------------------- // insert // ----------------------------------