Skip to content

Improper handling of NaN #932

Open
Open

Description

@jackiescanlon

Version

pymongo 3.11.0
mongomock 4.3.0

Steps to reproduce

  1. Create a python file with the following code:
import mongomock
import numpy as np
import pymongo


# @mongomock.patch(servers=(("localhost", 27017),))
def test_insert() -> None:
    coll = pymongo.MongoClient("mongodb://localhost:27017/my_db").my_db.my_collection

    # Insert some objects
    objects = [{"a": 1.0}, {"a": float("nan")}]
    coll.insert_many(objects)
    print(f"Number of documents: {coll.count_documents({})}")

    # Filter out objects that are NaN
    cursor = coll.find({"a": {"$ne": np.nan}})

    print("Documents: None of the following should be nan:")
    for doc in cursor:
        print(f'    {doc["a"]}')

    coll.delete_many({})


test_insert()
  1. Start a mongodb server at mongodb://localhost:27017
  2. Run the code

Result:

❯ python3 mongomock_test.py
Number of documents: 2
Documents: None of the following should be nan:
    1.0
  1. Stop the mongodb server
  2. Uncomment line 6 in the python file
  3. Run the code

Result:

❯ python3 mongomock_test.py
Number of documents: 2
Documents: None of the following should be nan:
    1.0
    nan

Expected result

I expected mongomock to filter out NaNs the way that pymongo does

Actual result

mongomock did not filter out any NaNs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions