From 947c2ac98dec4841bebdcad258d5095017cecba7 Mon Sep 17 00:00:00 2001 From: Juan David Date: Sun, 12 Jan 2025 22:00:58 +0300 Subject: [PATCH] feat: Add support for extended JSON in mongo node --- packages/nodes-base/nodes/MongoDb/GenericFunctions.ts | 5 +++++ packages/nodes-base/nodes/MongoDb/MongoDb.node.ts | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/MongoDb/GenericFunctions.ts b/packages/nodes-base/nodes/MongoDb/GenericFunctions.ts index 68edc03e1dcc5..ba8b537b721b9 100644 --- a/packages/nodes-base/nodes/MongoDb/GenericFunctions.ts +++ b/packages/nodes-base/nodes/MongoDb/GenericFunctions.ts @@ -1,3 +1,4 @@ +import {EJSON} from 'bson'; import get from 'lodash/get'; import set from 'lodash/set'; import { MongoClient, ObjectId } from 'mongodb'; @@ -145,6 +146,10 @@ export function stringifyObjectIDs(items: INodeExecutionData[]) { return items; } +export function parseJsonToEjson(query: unknown) { + return EJSON.parse(JSON.stringify(query)) as Record +} + export async function connectMongoClient(connectionString: string, credentials: IDataObject = {}) { let client: MongoClient; diff --git a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts index 349e56fd43c48..bb9c56fc20fa5 100644 --- a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts +++ b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts @@ -21,6 +21,7 @@ import type { import { buildParameterizedConnString, connectMongoClient, + parseJsonToEjson, prepareFields, prepareItems, stringifyObjectIDs, @@ -134,7 +135,7 @@ export class MongoDb implements INodeType { const query = mdb .collection(this.getNodeParameter('collection', i) as string) - .aggregate(queryParameter as unknown as Document[]); + .aggregate(parseJsonToEjson(queryParameter) as unknown as Document[]); for (const entry of await query.toArray()) { returnData.push({ json: entry, pairedItem: fallbackPairedItems ?? [{ item: i }] }); @@ -155,9 +156,13 @@ export class MongoDb implements INodeType { if (operation === 'delete') { for (let i = 0; i < itemsLength; i++) { try { + const queryParameter = JSON.parse( + this.getNodeParameter('query', i) as string, + ) as IDataObject; + const { deletedCount } = await mdb .collection(this.getNodeParameter('collection', i) as string) - .deleteMany(JSON.parse(this.getNodeParameter('query', i) as string) as Document); + .deleteMany(parseJsonToEjson(queryParameter) as unknown as Document); returnData.push({ json: { deletedCount }, @@ -189,7 +194,7 @@ export class MongoDb implements INodeType { let query = mdb .collection(this.getNodeParameter('collection', i) as string) - .find(queryParameter as unknown as Document); + .find(parseJsonToEjson(queryParameter) as unknown as Document); const options = this.getNodeParameter('options', i); const limit = options.limit as number;