Skip to content

Fix find data with projection is not immutable - #692

Open
duynguyenhoang wants to merge 1 commit into
mongomock:developfrom
duynguyenhoang:fix/immutable-data-projection
Open

duynguyenhoang wants to merge 1 commit into
mongomock:developfrom
duynguyenhoang:fix/immutable-data-projection

Conversation

@duynguyenhoang

@duynguyenhoang duynguyenhoang commented Jan 18, 2021 •

Copy link
Copy Markdown

When finding document with projection, currently data returned is mutable. It should be immutable.

How to reproduce:

# Prepare data
collection.insert_one({'_id': 1, 'years': {'last_job': 2010}})
item_before = collection.find_one({'_id': 1}, projection={'years': True})
# Try to update document dict, bug is here
item_before['years']['last_job'] = 2021

# Get item again
item_after = collection.find_one({'_id': 1})

# data should be 2010, but currently it is 2021
assert item_before['years']['last_job'] == 2010

Similar issue to old PR #643

@codecov

codecov Bot commented Jan 18, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #692 (b5f194f) into develop (4bb8dcf) will decrease coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #692      +/-   ##
===========================================
- Coverage    95.45%   95.42%   -0.03%     
===========================================
  Files           19       19              
  Lines         3605     3606       +1     
===========================================
  Hits          3441     3441              
- Misses         164      165       +1     
Impacted Files Coverage Δ
mongomock/collection.py 92.40% <100.00%> (-0.08%) ⬇️
mongomock/aggregate.py 97.56% <0.00%> (+<0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4bb8dcf...b5f194f. Read the comment docs.

@duynguyenhoang
duynguyenhoang force-pushed the fix/immutable-data-projection branch from 6817cc5 to b33012a Compare January 18, 2021 11:01
@duynguyenhoang
duynguyenhoang force-pushed the fix/immutable-data-projection branch from b33012a to b5f194f Compare January 18, 2021 12:05
@pcorpet

pcorpet commented Jan 18, 2021

Copy link
Copy Markdown
Member

Thanks for your PR. I have two concerns with this change:

  • you write about immutable data after projection, however I feel this should be true even when not projecting (just finding data). If you agree then we should copy at the last moment, not when projecting.
  • there might be some big drawbacks in performances if we copy big objects. Did you do some stress tests?

@duynguyenhoang

Copy link
Copy Markdown
Author
  • The implementation of finding data without copy is already immutable with copy, so we don't need to do copy at the last moment.
  • I haven't do any stress test, but you are right deepcopy is bad in performances, let me use another lightweight of deepcopy instead.

engFelipeMonteiro added a commit to engFelipeMonteiro/mongomock-ng that referenced this pull request May 21, 2026
Replicates mongomock#692 — closes #191 (immutable projection)
Replicates mongomock#795 — closes #184 ($in comparison)
Replicates mongomock#819 — closes #178 ($slice parsing)
Replicates mongomock#847 — closes #170 ($addToSet $each)
Replicates mongomock#860 — closes #169 ($redact support)
Replicates mongomock#895 — closes #158 (collation signature)
Replicates mongomock#919 — closes #154 ($in array matching)
Replicates mongomock#933 — closes #147 (BulkOpBuilder sort)
Replicates mongomock#936 — closes #145 (NaN handling)
Replicates mongomock#944 — closes #144 (utcnow deprecation)

Version bump: 7.4.0 -> 7.4.1
engFelipeMonteiro added a commit to engFelipeMonteiro/mongomock-ng that referenced this pull request May 21, 2026
## [7.4.1] - 2026-05-21
### Changed
- Drop Python 3.14 from test matrix and classifiers — not yet stable, incompatible with runtime dependencies
- Add pymongo 4.12.0 and 4.14.0 to hatch-test matrix for broader compatibility coverage

### Fixed
- TTL document expiry no longer crashes on Python 3.13 — normalize naive datetimes to UTC-aware before subtraction in `_value_meets_expiry` (store.py)
- Replace deprecated `datetime.utcfromtimestamp` with `datetime.fromtimestamp(..., tz=timezone.utc)` in aggregation `$convert` to date handlers (aggregate.py)
- Replace deprecated `datetime.utcnow()` with `datetime.now(timezone.utc).replace(tzinfo=None)` in helpers and test suite
- Fix hatch-test matrix so environment `hatch-test.*-4.11.0` correctly installs `pymongo==4.11.0` (missing `if` clause)
- `find()` projection no longer mutates original document data (mongomock#692 — closes #191)
- `$in` operator correctly handles empty-list values (mongomock#795 — closes #184)
- `$slice` aggregation operator evaluates arguments as expressions (mongomock#819 — closes #178)
- `$addToSet` with `$each` correctly deduplicates repeated values across runs (mongomock#847 — closes #170)
- `$redact` aggregation stage now supported (mongomock#860 — closes #169)
- `Cursor.collation` matches PyMongo 4.x method signature (mongomock#895 — closes #158)
- `$in` operator matches whole arrays in document values (mongomock#919 — closes #154)
- `BulkOperationBuilder.add_update` and `_update` support `sort` parameter (mongomock#933 — closes #147)
- NaN comparison handling in filter `$eq` operator (mongomock#936 — closes #145)
- Replace deprecated `datetime.utcnow` with `datetime.now(UTC)` on Python 3.11+ (mongomock#944 — closes #144)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants