Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

projecting a computed field from embedded documents returns wrong result #887

Open
slingshotvfx opened this issue Apr 17, 2024 · 0 comments

Comments

@slingshotvfx
Copy link

slingshotvfx commented Apr 17, 2024

When projecting a computed field from an embedded/nested document, mongomock returns the embedded object instead of the computed result.

Example document:

    document = {
        "a": "test_a",
        "embedded": {
            "embedded_key": "test_embedded",
        },
    }

querying that with projection={"embedded": "$embedded.embedded_key"} results in:

mongomock:

{'embedded': {'embedded_key': 'test_embedded'}}

the expected result is:

{'embedded': 'test_embedded'}

This bug is possibly related to/a dupe of #835

Full reproducible example:

import mongomock
import pymongo


def main(client: mongomock.MongoClient):
    collection = client["db_test"]["users"]

    document = {
        "a": "test_a",
        "embedded": {
            "embedded_key": "test_embedded",
        },
    }

    result = collection.insert_one(document)

    projection = {"embedded": "$embedded.embedded_key"}

    db_doc = collection.find_one({"_id": result.inserted_id}, projection=projection)
    client.close()
    return db_doc


if __name__ == "__main__":
    MONGODB_URI = "<your url here>"
    client = pymongo.MongoClient(host=MONGODB_URI)
    pymongo_res = main(client)

    client = mongomock.MongoClient()
    mongomock_res = main(client)

    print(pymongo_res)
    print(mongomock_res)
    assert pymongo_res == mongomock_res
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

No branches or pull requests

1 participant