diff --git a/apps/desktop/src/lib/mongo/mongoShellCommand.ts b/apps/desktop/src/lib/mongo/mongoShellCommand.ts index dfcbedb475..71283aa8fe 100644 --- a/apps/desktop/src/lib/mongo/mongoShellCommand.ts +++ b/apps/desktop/src/lib/mongo/mongoShellCommand.ts @@ -421,6 +421,16 @@ export function parseMongoWriteCommand(input: string): MongoWriteCommand | null return Array.isArray(JSON.parse(docs)) ? { kind: "insert", collection: insertMany.collection, docsJson: docs } : null; } + const insert = parseCollectionMethodTarget(source, "insert"); + if (insert) { + const args = parseMethodArgs(source, insert.methodCallIndex); + if (!args || args.length !== 1 || !args[0]?.trim()) return null; + const docs = normalizeJsonArgument(args[0]); + if (!docs) return null; + const value = JSON.parse(docs); + return value !== null && typeof value === "object" ? { kind: "insert", collection: insert.collection, docsJson: docs } : null; + } + for (const method of ["updateOne", "updateMany"] as const) { const target = parseCollectionMethodTarget(source, method); if (!target) continue; diff --git a/packages/app-tests/mongoShellCommand.test.ts b/packages/app-tests/mongoShellCommand.test.ts index efbce06355..601d67c16b 100644 --- a/packages/app-tests/mongoShellCommand.test.ts +++ b/packages/app-tests/mongoShellCommand.test.ts @@ -288,6 +288,33 @@ test("parseMongoWriteCommand accepts unquoted insert and update commands", () => }); }); +test("parseMongoWriteCommand accepts legacy insert commands", () => { + assert.deepEqual( + parseMongoWriteCommand(`db.getCollection("accounting_reconciliations").insert({ + "accountId": 999, + "status": "done" + });`), + { + kind: "insert", + collection: "accounting_reconciliations", + docsJson: '{\n "accountId": 999,\n "status": "done"\n }', + }, + ); + assert.deepEqual(parseMongoWriteCommand("db.products.insert([{name: 'first'}, {name: 'second'}])"), { + kind: "insert", + collection: "products", + docsJson: '[{"name": "first"}, {"name": "second"}]', + }); + assert.deepEqual(parseMongoWriteCommand("db.products.insertMany([{name: 'first'}, {name: 'second'}])"), { + kind: "insert", + collection: "products", + docsJson: '[{"name": "first"}, {"name": "second"}]', + }); + assert.equal(parseMongoWriteCommand("db.products.insert({name: 'demo'}, {writeConcern: {w: 1}})"), null); + assert.equal(parseMongoWriteCommand("db.products.insert()"), null); + assert.equal(parseMongoWriteCommand("db.products.insert('demo')"), null); +}); + test("parseMongoWriteCommand accepts updateMany arrayFilters options", () => { assert.deepEqual( parseMongoWriteCommand(`db.issue_3231.updateMany( diff --git a/packages/node-core/src/database.ts b/packages/node-core/src/database.ts index 8387371f5c..c5f4446e90 100644 --- a/packages/node-core/src/database.ts +++ b/packages/node-core/src/database.ts @@ -1613,6 +1613,16 @@ export function parseMongoWriteCommand(input: string): MongoWriteCommand | null return Array.isArray(JSON.parse(docs)) ? { kind: "insert", collection: insertMany.collection, docsJson: docs } : null; } + const insert = parseCollectionMethodTarget(source, "insert"); + if (insert) { + const args = parseMethodArgs(source, insert.methodCallIndex); + if (!args || args.length !== 1 || !args[0]?.trim()) return null; + const docs = normalizeJsonArgument(args[0]); + if (!docs) return null; + const value = JSON.parse(docs); + return value !== null && typeof value === "object" ? { kind: "insert", collection: insert.collection, docsJson: docs } : null; + } + for (const method of ["updateOne", "updateMany"] as const) { const target = parseCollectionMethodTarget(source, method); if (!target) continue; diff --git a/packages/node-core/tests/mongo-query.test.ts b/packages/node-core/tests/mongo-query.test.ts index 7631e55adb..bef222c5b9 100644 --- a/packages/node-core/tests/mongo-query.test.ts +++ b/packages/node-core/tests/mongo-query.test.ts @@ -295,6 +295,26 @@ test("parseMongoWriteCommand accepts supported write commands", () => { }); }); +test("parseMongoWriteCommand accepts legacy insert commands", () => { + assert.deepEqual(parseMongoWriteCommand('db.getCollection("accounting_reconciliations").insert({"accountId":999,"status":"done"});'), { + kind: "insert", + collection: "accounting_reconciliations", + docsJson: '{"accountId":999,"status":"done"}', + }); + assert.deepEqual(parseMongoWriteCommand('db.projects.insert([{"name":"first"},{"name":"second"}])'), { + kind: "insert", + collection: "projects", + docsJson: '[{"name":"first"},{"name":"second"}]', + }); + assert.deepEqual(parseMongoWriteCommand('db.projects.insertMany([{"name":"first"},{"name":"second"}])'), { + kind: "insert", + collection: "projects", + docsJson: '[{"name":"first"},{"name":"second"}]', + }); + assert.equal(parseMongoWriteCommand('db.projects.insert({"name":"demo"},{"writeConcern":{"w":1}})'), null); + assert.equal(parseMongoWriteCommand("db.projects.insert()"), null); +}); + test("parseMongoWriteCommand rejects invalid dropIndex and dropIndexes commands", () => { assert.equal(parseMongoWriteCommand("db.projects.dropIndex()"), null); assert.equal(parseMongoWriteCommand('db.projects.dropIndex("*")'), null);