Skip to content

Commit ab8b431

Browse files
committed
fix(BaseRaw): enhance upsert logic to ensure _id is set correctly when filtering updates
1 parent f9d85e6 commit ab8b431

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

packages/models/src/models/BaseRaw.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export abstract class BaseRaw<
174174
public findOneAndUpdate(query: Filter<T>, update: UpdateFilter<T> | T, options?: FindOneAndUpdateOptions): Promise<WithId<T> | null> {
175175
this.setUpdatedAt(update);
176176

177-
if (options?.upsert && !('_id' in update || (update.$set && '_id' in update.$set))) {
177+
if (options?.upsert && !('_id' in update || (update.$set && '_id' in update.$set)) && !('_id' in query)) {
178178
update.$setOnInsert = {
179179
...(update.$setOnInsert || {}),
180180
_id: new ObjectId().toHexString(),
@@ -251,7 +251,7 @@ export abstract class BaseRaw<
251251
updateOne(filter: Filter<T>, update: UpdateFilter<T>, options?: UpdateOptions): Promise<UpdateResult> {
252252
this.setUpdatedAt(update);
253253
if (options) {
254-
if (options.upsert && !('_id' in update || (update.$set && '_id' in update.$set))) {
254+
if (options.upsert && !('_id' in update || (update.$set && '_id' in update.$set)) && !('_id' in filter)) {
255255
update.$setOnInsert = {
256256
...(update.$setOnInsert || {}),
257257
_id: new ObjectId().toHexString(),

0 commit comments

Comments
 (0)