diff --git a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts index f939a1b9e2..ac9ad9af95 100644 --- a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts +++ b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts @@ -23,6 +23,7 @@ import { import { getItemCopy, handleDateFields, + handleDateFieldsWithDotNotation, validateAndResolveMongoCredentials } from './mongo.node.utils'; @@ -145,8 +146,10 @@ export class MongoDb implements INodeType { const options = this.getNodeParameter('options', 0) as IDataObject; const insertItems = getItemCopy(items, fields); - if (options.dateFields) { + if (options.dateFields && !options.useDotNotation) { handleDateFields(insertItems, options.dateFields as string); + } else if (options.dateFields && options.useDotNotation) { + handleDateFieldsWithDotNotation(insertItems, options.dateFields as string); } const { insertedIds } = await mdb @@ -194,8 +197,10 @@ export class MongoDb implements INodeType { // Prepare the data to update and copy it to be returned const updateItems = getItemCopy(items, fields); - if (options.dateFields) { + if (options.dateFields && !options.useDotNotation) { handleDateFields(updateItems, options.dateFields as string); + } else if (options.dateFields && options.useDotNotation) { + handleDateFieldsWithDotNotation(updateItems, options.dateFields as string); } for (const item of updateItems) { diff --git a/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts b/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts index 6ed8f2854a..2951436b54 100644 --- a/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts +++ b/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts @@ -266,7 +266,14 @@ export const nodeDescription: INodeTypeDescription = { name: 'dateFields', type: 'string', default: '', - description: 'Comma separeted list of fields that will be parse as Mongo Date type.', + description: 'Comma separeted list of fields that will be parse as Mongo Date type', + }, + { + displayName:'Use Dot Notation', + name: 'useDotNotation', + type: 'boolean', + default: false, + description: 'Wheather to use dot notation to access date fields', }, ], }, diff --git a/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts b/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts index 42dc4efcb0..be09378b09 100644 --- a/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts +++ b/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts @@ -11,6 +11,8 @@ import { IMongoParametricCredentials, } from './mongo.node.types'; +import { get, set } from 'lodash'; + /** * Standard way of building the MongoDB connection string, unless overridden with a provided string * @@ -118,3 +120,18 @@ export function handleDateFields(insertItems: IDataObject[], fields: string) { } } } + +export function handleDateFieldsWithDotNotation(insertItems: IDataObject[], fields: string) { + const dateFields = fields.split(',').map(field => field.trim()); + + for (let i = 0; i < insertItems.length; i++) { + for (const field of dateFields) { + const fieldValue = get(insertItems[i], field) as string; + const date = new Date(fieldValue); + + if (fieldValue && !isNaN(date.valueOf())) { + set(insertItems[i], field, date); + } + } + } +}