diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index a520cdb157..d24cb51fcc 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -118,6 +118,22 @@ module MongoDBModule { parseQueryParams(params: string, mode: string) { let queryParams = params.split(/(?<=}),[\n\s]*(?={)/g) + if (queryParams.length > 3) { + for (let i = 0; i < queryParams.length; i++) { + const openCount = queryParams[i].match(/{/g)?.length ?? 0 + const closeCount = queryParams[i].match(/}/g)?.length ?? 0 + if ((openCount + closeCount) % 2 !== 0) { + if (openCount > closeCount) { + queryParams[i] += `, ${queryParams[i+1]}` + queryParams.splice(i+1, 1) + } else { + queryParams[i-1] += `, ${queryParams[i]}` + queryParams.splice(i, 1) + i-- + } + } + } + } let group1 = queryParams[0] ? JSON.parse(queryParams[0]) : {} let group2 = queryParams[1] ? JSON.parse(queryParams[1]) : {} let group3 = queryParams[2] ? JSON.parse(queryParams[2]) : {} diff --git a/packages/server/src/integrations/tests/mongo.spec.js b/packages/server/src/integrations/tests/mongo.spec.js index 555188a526..17561723b3 100644 --- a/packages/server/src/integrations/tests/mongo.spec.js +++ b/packages/server/src/integrations/tests/mongo.spec.js @@ -214,4 +214,56 @@ describe("MongoDB Integration", () => { upsert: false }) }) + + it("can parse nested objects with arrays", async () => { + const query = { + json: `{ + "_id": { + "$eq": "ObjectId('ACBD12345678ABCD12345678')" + } + }, + { + "$set": { + "value": { + "data": [ + { "cid": 1 }, + { "cid": 2 }, + { "nested": { + "name": "test" + }} + ] + } + } + }, + { + "upsert": true + }`, + extra: { collection: "testCollection", actionTypes: "updateOne" }, + } + await config.integration.update(query) + expect(config.integration.client.updateOne).toHaveBeenCalled() + + const args = config.integration.client.updateOne.mock.calls[0] + expect(args[0]).toEqual({ + _id: { + $eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), + } + }) + expect(args[1]).toEqual({ + $set: { + value: { + data: [ + { cid: 1 }, + { cid: 2 }, + { nested: { + name: "test" + }} + ] + }, + }, + }) + expect(args[2]).toEqual({ + upsert: true + }) + }) })