fixed mongo update for mongo4 #916

This commit is contained in:
SPRINX0\prochazka
2024-10-15 10:38:44 +02:00
parent e54cffbaf3
commit 42601ff960
2 changed files with 21 additions and 17 deletions

View File

@@ -361,16 +361,17 @@ const driver = {
res.replaced.push(resdoc._id); res.replaced.push(resdoc._id);
} }
} else { } else {
const resdoc = await collection.updateOne(convertObjectId(update.condition), { const set = convertObjectId(_.pickBy(update.fields, (v, k) => !v?.$$undefined$$));
$set: convertObjectId(_.pickBy(update.fields, (v, k) => !v?.$$undefined$$)), const unset = _.fromPairs(
$unset: _.fromPairs( Object.keys(update.fields)
Object.keys(update.fields) .filter((k) => update.fields[k]?.$$undefined$$)
.filter((k) => update.fields[k]?.$$undefined$$) .map((k) => [k, ''])
.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); res.updated.push(resdoc._id);
} }
} }

View File

@@ -95,16 +95,19 @@ const driver = {
...update.fields, ...update.fields,
})});\n`; })});\n`;
} else { } 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( res += `db.${update.pureName}.updateOne(${jsonStringifyWithObjectId(
update.condition update.condition
)}, ${jsonStringifyWithObjectId({ )}, ${jsonStringifyWithObjectId(updates)});\n`;
$set: _pickBy(update.fields, (v, k) => v !== undefined),
$unset: _fromPairs(
Object.keys(update.fields)
.filter((k) => update.fields[k] === undefined)
.map((k) => [k, ''])
),
})});\n`;
} }
} }
for (const del of changeSet.deletes) { for (const del of changeSet.deletes) {