Repository navigation
Fix NaN handling in operator_eq to match MongoDB behavior - #936
Open
nmurrell07 wants to merge 1 commit into
Open
nmurrell07 wants to merge 1 commit into
nmurrell07 wants to merge 1 commit into
Conversation
MongoDB treats all NaN values as equal for comparison purposes. This fix ensures that: - $eq with NaN matches documents containing NaN values - $ne with NaN excludes documents containing NaN values This resolves issue mongomock#932 where mongomock was not filtering out NaN values correctly when using $ne operator.
This was referenced May 19, 2026
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes NaN (Not a Number) comparison behavior in mongomock to match MongoDB's semantics. In MongoDB, all NaN values are treated as equal for comparison purposes, but Python's default
==operator returnsFalsewhen comparing two NaN floats.The Problem
However, MongoDB treats all NaN values as equal:
This caused mongomock to fail when querying for NaN values or comparing documents containing NaN.
The Fix
Added a helper function
_is_nan()and updatedoperator_eq()to handle NaN comparisons specially:_is_nan(value): Checks if a value is a float and is NaN usingmath.isnan()operator_eq(doc_val, search_val): Now returnsTruewhen both values are NaN, before falling back to standard equality comparisonThis ensures mongomock's filtering behavior matches MongoDB's actual semantics.
Verification
Tested with:
{field: float('nan')})Limitations
floatNaN values; other potential NaN representations would need separate handling if discoveredNote: The issue description referenced Connection constructor arguments, but this PR addresses the actual code changes in the diff (NaN comparison handling). If you'd like me to also address the Connection constructor compatibility, I can create a separate PR for that.