diff --git a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts index e9abdc2bd2..c03a62816f 100644 --- a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts +++ b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts @@ -1,14 +1,26 @@ -import { IExecuteFunctions } from 'n8n-core'; +import { + IExecuteFunctions, +} from 'n8n-core'; + import { IDataObject, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow'; -import { nodeDescription } from './mongo.node.options'; -import { MongoClient } from 'mongodb'; + +import { + nodeDescription, +} from './mongo.node.options'; + +import { + MongoClient, + ObjectID, +} from 'mongodb'; + import { getItemCopy, + handleDateFields, validateAndResolveMongoCredentials } from './mongo.node.utils'; @@ -65,8 +77,13 @@ export class MongoDb implements INodeType { .map(f => f.trim()) .filter(f => !!f); + const options = this.getNodeParameter('options', 0) as IDataObject; const insertItems = getItemCopy(items, fields); + if (options.dateFields) { + handleDateFields(insertItems, options.dateFields as string); + } + const { insertedIds } = await mdb .collection(this.getNodeParameter('collection', 0) as string) .insertMany(insertItems); @@ -90,6 +107,8 @@ export class MongoDb implements INodeType { .map(f => f.trim()) .filter(f => !!f); + const options = this.getNodeParameter('options', 0) as IDataObject; + let updateKey = this.getNodeParameter('updateKey', 0) as string; updateKey = updateKey.trim(); @@ -100,19 +119,25 @@ export class MongoDb implements INodeType { // Prepare the data to update and copy it to be returned const updateItems = getItemCopy(items, fields); + if (options.dateFields) { + handleDateFields(updateItems, options.dateFields as string); + } + for (const item of updateItems) { if (item[updateKey] === undefined) { continue; } - const filter: { [key: string]: string } = {}; + const filter: { [key: string]: string | ObjectID } = {}; filter[updateKey] = item[updateKey] as string; - + if (updateKey === '_id') { + filter[updateKey] = new ObjectID(filter[updateKey]); + delete item['_id']; + } await mdb .collection(this.getNodeParameter('collection', 0) as string) .updateOne(filter, { $set: item }); } - returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]); } else { throw new Error(`The operation "${operation}" is not supported!`); diff --git a/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts b/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts index d4bd0a37b5..ec09998450 100644 --- a/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts +++ b/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts @@ -1,4 +1,6 @@ -import { INodeTypeDescription } from 'n8n-workflow'; +import { + INodeTypeDescription, +} from 'n8n-workflow'; /** * Options to be displayed @@ -6,7 +8,7 @@ import { INodeTypeDescription } from 'n8n-workflow'; export const nodeDescription: INodeTypeDescription = { displayName: 'MongoDB', name: 'mongoDb', - icon: 'file:mongoDb.png', + icon: 'file:mongoDb.svg', group: ['input'], version: 1, description: 'Find, insert and update documents in MongoDB.', @@ -46,7 +48,7 @@ export const nodeDescription: INodeTypeDescription = { { name: 'Update', value: 'update', - description: 'Updates documents.', + description: 'Update documents.', }, ], default: 'find', @@ -97,7 +99,9 @@ export const nodeDescription: INodeTypeDescription = { }, displayOptions: { show: { - operation: ['find'], + operation: [ + 'find', + ], }, }, default: '{}', @@ -115,7 +119,9 @@ export const nodeDescription: INodeTypeDescription = { type: 'string', displayOptions: { show: { - operation: ['insert'], + operation: [ + 'insert', + ], }, }, default: '', @@ -133,7 +139,9 @@ export const nodeDescription: INodeTypeDescription = { type: 'string', displayOptions: { show: { - operation: ['update'], + operation: [ + 'update', + ], }, }, default: 'id', @@ -147,7 +155,9 @@ export const nodeDescription: INodeTypeDescription = { type: 'string', displayOptions: { show: { - operation: ['update'], + operation: [ + 'update', + ], }, }, default: '', @@ -155,5 +165,29 @@ export const nodeDescription: INodeTypeDescription = { description: 'Comma separated list of the fields to be included into the new document.', }, + { + displayName: 'Options', + name: 'options', + type: 'collection', + displayOptions: { + show: { + operation: [ + 'update', + 'insert', + ], + }, + }, + placeholder: 'Add Option', + default: {}, + options: [ + { + displayName: 'Date Fields', + name: 'dateFields', + type: 'string', + default: '', + description: 'Comma separeted list of fields that will be parse as Mongo Date type.', + }, + ], + }, ], }; diff --git a/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts b/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts index bc2d308a76..87fefd69b3 100644 --- a/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts +++ b/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts @@ -102,3 +102,14 @@ export function getItemCopy( return newItem; }); } + +export function handleDateFields(insertItems: IDataObject[], fields: string) { + const dateFields = (fields as string).split(','); + for (let i = 0; i < insertItems.length; i++) { + for (const key of Object.keys(insertItems[i])) { + if (dateFields.includes(key)) { + insertItems[i][key] = new Date(insertItems[i][key] as string); + } + } + } +} diff --git a/packages/nodes-base/nodes/MongoDb/mongoDb.png b/packages/nodes-base/nodes/MongoDb/mongoDb.png deleted file mode 100644 index 0980afcb2b..0000000000 Binary files a/packages/nodes-base/nodes/MongoDb/mongoDb.png and /dev/null differ diff --git a/packages/nodes-base/nodes/MongoDb/mongodb.svg b/packages/nodes-base/nodes/MongoDb/mongodb.svg new file mode 100644 index 0000000000..80d3a99ac2 --- /dev/null +++ b/packages/nodes-base/nodes/MongoDb/mongodb.svg @@ -0,0 +1 @@ + \ No newline at end of file