Skip to content

Not founding key in document even though key exists #116

Description

@azeredo-e

I have a collection and in each document there are a certain set of fields, but not all documents contain all fields. For example, in collection "foo" we can have three documents:

{"client_name": "john doe",
 "product": "car",
 "car_colour": "red"},
{"client_name": "jane doe",
 "product": "bike",
 "bike_colour": "blue"},
{"client_name": "jack doe"
 "product": NULL}

Each one is a client that bought a certain product and because of that other fields reflect the product that he bought and not every document has every single field.

If I was trying to retrieve all documents i would use:

filter = Mongoc.BSON("""{"projection": {"car_colour": true}}""")
res = Mongoc.find(foo, options=filter)

It would return an error stating: KeyError: key "car_colour" not found

I was expecting to it return all documents and in those docs that have fields that don't exists in other documents, just return empty in that field. From my experience Python does something similar with PyMongo.

Julia: 1.9.1 Mongoc: 0.9.0

edit: Added version of Julia and Mongoc

Activity

  1. quinnj commented on Oct 4, 2026

    @quinnj
    Member

    I tested the same three-document collection against MongoDB 5.0.33 with current master 4c7bf1f4a95906b9670243193ead42507138ab97 and MongoC_jll 1.30.8. Mongoc.find(foo; options=filter) and collecting its cursor both succeed. The projection returns:

    BSON("_id" => 1, "car_colour" => "red")
    BSON("_id" => 2)
    BSON("_id" => 3)

    The field is absent from the second and third documents. Direct indexing such as doc["car_colour"] then throws KeyError; use the existing get method when the field is optional:

    filter = Mongoc.BSON("{\"projection\": {\"car_colour\": true}}")
    colours = [get(doc, "car_colour", nothing)
               for doc in Mongoc.find(foo; options=filter)]
    # ["red", nothing, nothing]

    get(doc, "car_colour", missing) also works if you prefer missing as the fallback. All 12 live-server checks passed, including the two expected direct-indexing errors and both fallback values. The lookup behavior is implemented here.

    If the error occurs in find or cursor iteration itself, before any field indexing, the full stack trace would help identify the remaining difference. I am leaving the issue open because that call location is not shown in the report.

    AI disclosure: This work was prepared with assistance from OpenAI Codex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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