Not founding key in document even though key exists #116
Description
Activity
I tested the same three-document collection against MongoDB 5.0.33 with current master
4c7bf1f4a95906b9670243193ead42507138ab97and 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 throwsKeyError; use the existinggetmethod 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 prefermissingas 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
findor 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.
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:
It would return an error stating:
KeyError: key "car_colour" not foundI 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