Frequently Asked Questions
This section answers questions that may arise when using the BSON data format.
How do I prevent the "IllegalArgumentException: Invalid BSON field name" error?
Your application might throw this exception if you pass an incorrectly formatted document to an operation and you are using Java driver v4.7 or earlier.
Note
In driver versions v4.8 and later, this error message was replaced by one that includes more specific details on what was incorrectly formatted.
For example, the driver throws this error when you call an update operation and incorrectly omit the update operator as shown in the following code example:
// incorrectly formatted update document collection.updateOne( new Document().append("name", "fizz"), new Document().append("name", "buzz") );
To avoid this error, use the builder class for the appropriate operation. The driver offers builder classes to create syntactically correct BSON for MongoDB operations. The preceding example can be correctly expressed using the builder classes, as shown in the following code example:
// Builder class imports import static com.mongodb.client.model.Filters.*; import static com.mongodb.client.model.Updates.*; // ... collection.updateOne(eq("name", "fizz"), set("name", "buzz"));
To learn more about the available builders classes, see the Builders guide.