diff --git a/packages/nodes-base/nodes/MongoDb/GenericFunctions.test.ts b/packages/nodes-base/nodes/MongoDb/GenericFunctions.test.ts new file mode 100644 index 0000000000..73bf1dcc6f --- /dev/null +++ b/packages/nodes-base/nodes/MongoDb/GenericFunctions.test.ts @@ -0,0 +1,80 @@ +import { prepareItems } from './GenericFunctions'; + +describe('MongoDB Node: Generic Functions', () => { + describe('prepareItems', () => { + it('should select fields', () => { + const items = [{ json: { name: 'John', age: 30 } }, { json: { name: 'Jane', age: 25 } }]; + const fields = ['name']; + + const result = prepareItems({ items, fields }); + + expect(result).toEqual([{ name: 'John' }, { name: 'Jane' }]); + }); + + it('should add updateKey to selected fields', () => { + const items = [{ json: { name: 'John', age: 30 } }, { json: { name: 'Jane', age: 25 } }]; + const fields = ['age']; + const updateKey = 'name'; + + const result = prepareItems({ items, fields, updateKey }); + + expect(result).toEqual([ + { name: 'John', age: 30 }, + { name: 'Jane', age: 25 }, + ]); + }); + + it('should handle dot notation', () => { + const items = [{ json: { user: { name: 'John' } } }, { json: { user: { name: 'Jane' } } }]; + const fields = ['user.name']; + const useDotNotation = true; + + const result = prepareItems({ items, fields, updateKey: '', useDotNotation }); + + expect(result).toEqual([{ user: { name: 'John' } }, { user: { name: 'Jane' } }]); + }); + + it('should parse dates', () => { + const items = [ + { json: { date: '2023-10-01T00:00:00Z' } }, + { json: { date: '2023-10-02T00:00:00Z' } }, + ]; + const fields = ['date']; + const dateFields = ['date']; + const useDotNotation = false; + const isUpdate = false; + const result = prepareItems({ + items, + fields, + updateKey: '', + useDotNotation, + dateFields, + isUpdate, + }); + expect(result).toEqual([ + { date: new Date('2023-10-01T00:00:00Z') }, + { date: new Date('2023-10-02T00:00:00Z') }, + ]); + }); + + it('should handle updates', () => { + // Should keep dot notation in result to not overwrite the original values + const items = [ + { json: { id: 1, user: { name: 'John', age: 30 } } }, + { json: { id: 2, user: { name: 'Jane', age: 25 } } }, + ]; + const fields = ['user.name']; + const useDotNotation = true; + const isUpdate = true; + const result = prepareItems({ + items, + fields, + updateKey: '', + useDotNotation, + dateFields: [], + isUpdate, + }); + expect(result).toEqual([{ 'user.name': 'John' }, { 'user.name': 'Jane' }]); + }); + }); +}); diff --git a/packages/nodes-base/nodes/MongoDb/GenericFunctions.ts b/packages/nodes-base/nodes/MongoDb/GenericFunctions.ts index 68edc03e1d..df5dacc19e 100644 --- a/packages/nodes-base/nodes/MongoDb/GenericFunctions.ts +++ b/packages/nodes-base/nodes/MongoDb/GenericFunctions.ts @@ -80,13 +80,21 @@ export function validateAndResolveMongoCredentials( } } -export function prepareItems( - items: INodeExecutionData[], - fields: string[], +export function prepareItems({ + items, + fields, updateKey = '', useDotNotation = false, - dateFields: string[] = [], -) { + dateFields = [], + isUpdate = false, +}: { + items: INodeExecutionData[]; + fields: string[]; + updateKey?: string; + useDotNotation?: boolean; + dateFields?: string[]; + isUpdate?: boolean; +}) { let data = items; if (updateKey) { @@ -112,7 +120,7 @@ export function prepareItems( fieldData = new Date(fieldData as string); } - if (useDotNotation) { + if (useDotNotation && !isUpdate) { set(updateItem, field, fieldData); } else { updateItem[field] = fieldData; diff --git a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts index bc39e877da..86afe14690 100644 --- a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts +++ b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts @@ -36,7 +36,7 @@ export class MongoDb implements INodeType { name: 'mongoDb', icon: 'file:mongodb.svg', group: ['input'], - version: [1, 1.1], + version: [1, 1.1, 1.2], description: 'Find, insert and update documents in MongoDB', defaults: { name: 'MongoDB', @@ -248,7 +248,7 @@ export class MongoDb implements INodeType { ? { upsert: true } : undefined; - const updateItems = prepareItems(items, fields, updateKey, useDotNotation, dateFields); + const updateItems = prepareItems({ items, fields, updateKey, useDotNotation, dateFields }); for (const item of updateItems) { try { @@ -290,7 +290,14 @@ export class MongoDb implements INodeType { ? { upsert: true } : undefined; - const updateItems = prepareItems(items, fields, updateKey, useDotNotation, dateFields); + const updateItems = prepareItems({ + items, + fields, + updateKey, + useDotNotation, + dateFields, + isUpdate: nodeVersion >= 1.2, + }); for (const item of updateItems) { try { @@ -329,7 +336,13 @@ export class MongoDb implements INodeType { this.getNodeParameter('options.dateFields', 0, '') as string, ); - const insertItems = prepareItems(items, fields, '', useDotNotation, dateFields); + const insertItems = prepareItems({ + items, + fields, + updateKey: '', + useDotNotation, + dateFields, + }); const { insertedIds } = await mdb .collection(this.getNodeParameter('collection', 0) as string) @@ -370,7 +383,14 @@ export class MongoDb implements INodeType { ? { upsert: true } : undefined; - const updateItems = prepareItems(items, fields, updateKey, useDotNotation, dateFields); + const updateItems = prepareItems({ + items, + fields, + updateKey, + useDotNotation, + dateFields, + isUpdate: nodeVersion >= 1.2, + }); for (const item of updateItems) { try {