fix: remove day from $setOnInsert — and $setOnInsert conflict on same path

MongoDB error: 'Updating the path day would create a conflict at day'.
On insert, $inc treats a missing field as 0, so no seed needed.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
TCFDONMEZ
2026-10-09 21:30:21 +03:00
co-authored by Claude
parent 6767293322
commit 8b25799d55
+3 -6
View File
@@ -11,9 +11,9 @@ export const dynamic = "force-dynamic";
* (not a cumulative total), so each PUT adds to the day's running totals rather than
* overwriting them. Lifetime is a snapshot — the latest one wins, so it is $set not $inc.
*
* $setOnInsert seeds the whole `day` subdocument as a nested object (not dotted paths)
* because $inc and $setOnInsert cannot touch the same field — MongoDB rejects that with
* a 500, which is what held the 20 cached chunks back.
* $setOnInsert must not touch `day` at all: $inc on `day.km` and $setOnInsert on `day`
* conflict in MongoDB ("Updating the path 'day' would create a conflict at 'day'").
* On an insert, $inc treats a missing field as 0 and adds to it, so no seed is needed.
*/
export async function PUT(request: Request) {
const denied = requireIngest(request);
@@ -38,9 +38,6 @@ export async function PUT(request: Request) {
$set,
$setOnInsert: {
createdAt: updatedAt,
installationId: record.installationId,
date: record.date,
day: { km: 0, kwh: 0, hours: 0, socDrop: 0 },
},
},
{ upsert: true },