support remove fields for mongo

This commit is contained in:
Jan Prochazka
2024-08-26 13:25:51 +02:00
parent 8e17516d54
commit 32ebd86171
6 changed files with 63 additions and 9 deletions

View File

@@ -330,7 +330,14 @@ const driver = {
}
} else {
const resdoc = await collection.updateOne(convertObjectId(update.condition), {
$set: convertObjectId(update.fields),
$set: convertObjectId(_.pickBy(update.fields, (v, k) => !v?.$undefined)),
$unset: _.fromPairs(
Object.keys(update.fields)
.filter((k) => update.fields[k]?.$undefined)
.map((k) => [k, ''])
),
// $set: convertObjectId(update.fields),
});
res.updated.push(resdoc._id);
}

View File

@@ -2,6 +2,8 @@ const { driverBase } = global.DBGATE_PACKAGES['dbgate-tools'];
const { convertToMongoCondition, convertToMongoSort } = require('./convertToMongoCondition');
const Dumper = require('./Dumper');
const { mongoSplitterOptions } = require('dbgate-query-splitter/lib/options');
const _pickBy = require('lodash/pickBy');
const _fromPairs = require('lodash/fromPairs');
function jsonStringifyWithObjectId(obj) {
return JSON.stringify(obj, undefined, 2).replace(
@@ -96,7 +98,12 @@ const driver = {
res += `db.${update.pureName}.updateOne(${jsonStringifyWithObjectId(
update.condition
)}, ${jsonStringifyWithObjectId({
$set: update.fields,
$set: _pickBy(update.fields, (v, k) => v !== undefined),
$unset: _fromPairs(
Object.keys(update.fields)
.filter((k) => update.fields[k] === undefined)
.map((k) => [k, ''])
),
})});\n`;
}
}
@@ -140,6 +147,8 @@ const driver = {
supportJsonType: true,
supportObjectIdType: true,
supportNullType: true,
supportFieldRemoval: true,
},
};