From 0ecda93ea9174e6d8cd6825c1701d3e9033c5e18 Mon Sep 17 00:00:00 2001 From: melohagan <101575380+melohagan@users.noreply.github.com> Date: Mon, 26 Jun 2023 15:07:29 +0100 Subject: [PATCH] Fix for $in on ObjectIds (#11014) --- packages/server/src/integrations/mongodb.ts | 27 ++++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index 3f05dca954..e716f3192d 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -380,18 +380,26 @@ class MongoIntegration implements IntegrationBase { return this.client.connect() } + matchId(value?: string) { + return value?.match(/(?<=objectid\(['"]).*(?=['"]\))/gi)?.[0] + } + + hasObjectId(value?: any): boolean { + return ( + typeof value === "string" && value.toLowerCase().startsWith("objectid") + ) + } + createObjectIds(json: any): object { const self = this + function interpolateObjectIds(json: any) { for (let field of Object.keys(json || {})) { if (json[field] instanceof Object) { json[field] = self.createObjectIds(json[field]) } - if ( - typeof json[field] === "string" && - json[field].toLowerCase().startsWith("objectid") - ) { - const id = json[field].match(/(?<=objectid\(['"]).*(?=['"]\))/gi)?.[0] + if (self.hasObjectId(json[field])) { + const id = self.matchId(json[field]) if (id) { json[field] = ObjectId.createFromHexString(id) } @@ -402,7 +410,14 @@ class MongoIntegration implements IntegrationBase { if (Array.isArray(json)) { for (let i = 0; i < json.length; i++) { - json[i] = interpolateObjectIds(json[i]) + if (self.hasObjectId(json[i])) { + const id = self.matchId(json[i]) + if (id) { + json[i] = ObjectId.createFromHexString(id) + } + } else { + json[i] = interpolateObjectIds(json[i]) + } } return json }