feat(MongoDb Node): Add Aggregate Operation

* MongoDB Aggregate Option

*  small improvements to UI

Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
Luiz Eduardo de Oliveira Fonseca 2022-03-12 16:01:44 -03:00 committed by GitHub
parent 195f104ef5
commit 2c9a06e863
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 5 deletions

View file

@ -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;

View file

@ -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 <a href="https://docs.mongodb.com/manual/core/aggregation-pipeline/">here</a>',
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
// ----------------------------------