[CXX-2120] Handling of nonexistent document fields is frustrating Created: 15/Oct/20  Updated: 28/Oct/23  Resolved: 30/Jun/23

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 3.9.0

Type: Improvement Priority: Major - P3
Reporter: Samantha Ritter (Inactive) Assignee: Josh Siegel (Inactive)
Resolution: Fixed Votes: 0
Labels: rb-track
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: CXX usability improvements
Quarter: FY24Q2

 Description   

Consider a document doc =

{ "secret" : "message" }

. If I attempt to access a non-existent field doc["sceret"], either because I made a typo in my code or for another reason, then use the resulting document::element, the driver throws a vague and unhelpful exception.

The root problem here is that the code that throws the exception runs when I attempt to use the result of my field lookup, not when I perform the lookup itself:

auto e = doc["sceret"]; // this returns an unset element e, but does not throw
auto value = e.get_value(); // this line throws, because e is an unset element

Once my unset element e is created, it loses any attachment to my original query (with the problematic nonexistent typo field "sceret"). This makes it very hard to improve the user experience with nonexistent fields. It makes it impossible for us to provide a more helpful exception message (like "document field 'sceret' does not exist").

The most common use case probably looks like this, with method chaining after the field lookup:

auto some_message = doc["sceret"].get_string().value;

Exception stack tracing could help in this use case, because the line with the offending lookup would be included in the trace. However, the exception will still have been thrown from a different place, in the get_string() method, which could be confusing for users.

document's operator[] used to throw, but several tickets removed that behavior:
https://jira.mongodb.org/browse/CXX-862
https://jira.mongodb.org/browse/CXX-1089

Those tickets discuss their reasons for removing throwing from operator[], which include wanting to remove inconsistencies between different classes in the driver. However, we should revisit these discussions and decide whether those arguments outweigh the sheer frustration of debugging typos in document lookup, and how that plays into the overall frustration users experience with the driver.



 Comments   
Comment by Githook User [ 30/Jun/23 ]

Author:

{'name': 'Joshua Siegel', 'email': '39130209+joshbsiegel@users.noreply.github.com', 'username': 'joshbsiegel'}

Message: CXX-2120 handle nonexistent document fields (#984)
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/16777df25be84b567fb4606a6f09569462faa4d3

Comment by Kevin Albertson [ 19/Oct/20 ]

We can reopen the discussion on this. Another advantage of not throwing on non-existent field lookup is having a convenient way of checking for existence:

if (doc["some_field"]) {
    // some_field exists
}

Generated at Wed Feb 07 22:04:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.