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:
@@ -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
|
* (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.
|
* 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)
|
* $setOnInsert must not touch `day` at all: $inc on `day.km` and $setOnInsert on `day`
|
||||||
* because $inc and $setOnInsert cannot touch the same field — MongoDB rejects that with
|
* conflict in MongoDB ("Updating the path 'day' would create a conflict at 'day'").
|
||||||
* a 500, which is what held the 20 cached chunks back.
|
* 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) {
|
export async function PUT(request: Request) {
|
||||||
const denied = requireIngest(request);
|
const denied = requireIngest(request);
|
||||||
@@ -38,9 +38,6 @@ export async function PUT(request: Request) {
|
|||||||
$set,
|
$set,
|
||||||
$setOnInsert: {
|
$setOnInsert: {
|
||||||
createdAt: updatedAt,
|
createdAt: updatedAt,
|
||||||
installationId: record.installationId,
|
|
||||||
date: record.date,
|
|
||||||
day: { km: 0, kwh: 0, hours: 0, socDrop: 0 },
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ upsert: true },
|
{ upsert: true },
|
||||||
|
|||||||
Reference in New Issue
Block a user