diff --git a/plugins/dbgate-plugin-mongo/src/backend/driver.js b/plugins/dbgate-plugin-mongo/src/backend/driver.js index b2e76ccac..b0d857792 100644 --- a/plugins/dbgate-plugin-mongo/src/backend/driver.js +++ b/plugins/dbgate-plugin-mongo/src/backend/driver.js @@ -361,16 +361,17 @@ const driver = { res.replaced.push(resdoc._id); } } else { - const resdoc = await collection.updateOne(convertObjectId(update.condition), { - $set: convertObjectId(_.pickBy(update.fields, (v, k) => !v?.$$undefined$$)), - $unset: _.fromPairs( - Object.keys(update.fields) - .filter((k) => update.fields[k]?.$$undefined$$) - .map((k) => [k, '']) - ), + const set = convertObjectId(_.pickBy(update.fields, (v, k) => !v?.$$undefined$$)); + const unset = _.fromPairs( + Object.keys(update.fields) + .filter((k) => update.fields[k]?.$$undefined$$) + .map((k) => [k, '']) + ); + const updates = {}; + if (!_.isEmpty(set)) updates.$set = set; + if (!_.isEmpty(unset)) updates.$unset = unset; - // $set: convertObjectId(update.fields), - }); + const resdoc = await collection.updateOne(convertObjectId(update.condition), updates); res.updated.push(resdoc._id); } } diff --git a/plugins/dbgate-plugin-mongo/src/frontend/driver.js b/plugins/dbgate-plugin-mongo/src/frontend/driver.js index 58e34cc22..45d3f66b3 100644 --- a/plugins/dbgate-plugin-mongo/src/frontend/driver.js +++ b/plugins/dbgate-plugin-mongo/src/frontend/driver.js @@ -95,16 +95,19 @@ const driver = { ...update.fields, })});\n`; } else { + const set = _pickBy(update.fields, (v, k) => v !== undefined); + const unset = _fromPairs( + Object.keys(update.fields) + .filter((k) => update.fields[k] === undefined) + .map((k) => [k, '']) + ); + const updates = {}; + if (!_.isEmpty(set)) updates.$set = set; + if (!_.isEmpty(unset)) updates.$unset = unset; + res += `db.${update.pureName}.updateOne(${jsonStringifyWithObjectId( update.condition - )}, ${jsonStringifyWithObjectId({ - $set: _pickBy(update.fields, (v, k) => v !== undefined), - $unset: _fromPairs( - Object.keys(update.fields) - .filter((k) => update.fields[k] === undefined) - .map((k) => [k, '']) - ), - })});\n`; + )}, ${jsonStringifyWithObjectId(updates)});\n`; } } for (const del of changeSet.deletes) {