[JAVA-2114] Better error message for "Invalid BSON field name" Created: 11/Feb/16  Updated: 26/Jul/22  Resolved: 26/Jul/22

Status: Closed
Project: Java Driver
Component/s: Error Handling
Affects Version/s: 3.2.0
Fix Version/s: 4.8.0

Type: Improvement Priority: Minor - P4
Reporter: Aleksey Korolev Assignee: Jeffrey Yemin
Resolution: Done Votes: 4
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I'm getting java.lang.IllegalArgumentException: "Invalid BSON field name" for update operation.



 Comments   
Comment by Githook User [ 26/Jul/22 ]

Author:

{'name': 'Jeff Yemin', 'email': 'jeff.yemin@mongodb.com', 'username': 'jyemin'}

Message: Improve error message for invalid field names (#985)

For replacement documents, it's now:
"Field names in a replacement document can not start with '$' but '%s' does"
For update documents, it's now:
"All update operators must start with '$', but '%s' does not"

JAVA-2114
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/6693493aab3ec178feb2470aac1190b52d39e7e7

Comment by Jeffrey Yemin [ 27/Mar/19 ]

There is an undeprecated variant of replaceOne: replaceOne(Bson filter, TDocument replacement, ReplaceOptions replaceOptions)

Comment by Peter Williamson [ 27/Mar/19 ]

Using replaceOne is a viable alternative if you want to replace the whole document, but it's marked as deprecated in the 3.7 driver. So this bug should be fixed or replaceOne reinstated. 

Comment by Peter Williamson [ 27/Mar/19 ]

IMHO this is incorrect validation. In the mongo client you can update without needing $set, db.collection.update( { _id: key}, { _id: key, field: value } ) works fine, but fails in the java driver with the "Invalid BSON field name _id"  exception.  The same error occurs with upsert=true and having to use $set with an upsert makes no sense. 

Comment by Michael J Winters [ 04/Mar/18 ]

here's a PR https://github.com/mongodb/mongo-java-driver/pull/460.
I also cloned this issue on accident ( https://jira.mongodb.org/browse/JAVA-2798 ) sorry about that

Comment by Naga Mayakuntla [ 16/Aug/17 ]

Any update on this?

Comment by Jeffrey Yemin [ 11/Feb/16 ]

OK, I see now. Thanks for reporting this. We'll see what we can do in a future release to address this.

Comment by Aleksey Korolev [ 11/Feb/16 ]

Sorry for not giving enough detail.
My error happened during collection.update(...) call after UpdateFieldNameValidator checked that field name doesn't start with $.
I reproduced error one more time and it indeed gave me field name:
java.lang.IllegalArgumentException: Invalid BSON field name customerID
Probably I overlooked it 1st time.
But it would be nice to have each validator provide details on what exactly it doesn't like about the field.
For example in this particular case I didn't know MongoDB update semantics good enough to quickly undersand the reason before I dug into code.
A resulting message like
"Invalid BSON field name customerID. Missing $ name prefix."
would be nice.

Comment by Jeffrey Yemin [ 11/Feb/16 ]

Looks like the message is generated from here, which does include the field name.

Can you provide the full stack trace?

Generated at Thu Feb 08 08:56:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.