Skip to content

Commit 609e460

Browse files
committed
fix(data_mongodb): remove illegal _id field from update payload
- Update the `update` method to remove the `_id` field from the document payload - Improve logging for update operations - Refactor the update process to ensure the item's ID is validated before proceeding
1 parent b121b84 commit 609e460

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/src/data_mongodb.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,12 @@ class DataMongodb<T> implements DataClient<T> {
483483
final selector = <String, dynamic>{'_id': ObjectId.fromHexString(id)};
484484
_logger.finer('Using update selector: $selector');
485485

486-
// Prepare the document for update. Note that the `_id` from the item
487-
// is ignored here, as the update is targeted by the `id` parameter.
488-
final docToUpdate = _toJson(item)..remove('id');
486+
// Prepare the document for update. This ensures the item's ID is
487+
// validated before proceeding.
488+
final docToUpdate = _prepareDocumentForInsertionOrUpdate(item)
489+
// The `_id` field must be removed from the update payload itself,
490+
// as it's illegal to modify the `_id` of an existing document.
491+
..remove('_id');
489492
_logger.finer('Update payload: $docToUpdate');
490493

491494
// Use findAndModify for an atomic update and return operation.

0 commit comments

Comments
 (0)