Skip to content

Commit

Permalink
feat(MongoDb Node): Add Aggregate Operation
Browse files Browse the repository at this point in the history
* MongoDB Aggregate Option

* ⚡ small improvements to UI

Co-authored-by: Michael Kret <michael.k@radency.com>
  • Loading branch information
luizeof and michael-radency authored Mar 12, 2022
1 parent 195f104 commit 2c9a06e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
37 changes: 32 additions & 5 deletions packages/nodes-base/nodes/MongoDb/MongoDb.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
INodeExecutionData,
INodeType,
INodeTypeDescription,
JsonObject,
NodeOperationError
} from 'n8n-workflow';

Expand Down Expand Up @@ -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
// ----------------------------------
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions packages/nodes-base/nodes/MongoDb/mongo.node.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export const nodeDescription: INodeTypeDescription = {
name: 'operation',
type: 'options',
options: [
{
name: 'Aggregate',
value: 'aggregate',
description: 'Aggregate documents.',
},
{
name: 'Delete',
value: 'delete',
Expand Down Expand Up @@ -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 <a href="https://docs.mongodb.com/manual/core/aggregation-pipeline/">here</a>',
required: true,
description: 'MongoDB aggregation pipeline query in JSON format',
},

// ----------------------------------
// delete
// ----------------------------------
Expand Down Expand Up @@ -149,6 +178,7 @@ export const nodeDescription: INodeTypeDescription = {
required: true,
description: 'MongoDB Find query.',
},

// ----------------------------------
// insert
// ----------------------------------
Expand Down

0 comments on commit 2c9a06e

Please sign in to comment.