Skip to content

Commit a81a86c

Browse files
author
Chris Cho
committed
DOCSP-26369: FAQ entry for invalid bson field name (#420)
* DOCSP-26369: Invalid BSON field name (cherry picked from commit f387ff6)
1 parent c1c86b2 commit a81a86c

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

source/faq.txt

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
FAQ
33
===
44

5-
.. default-domain:: mongodb
6-
75
.. contents:: On this page
86
:local:
97
:backlinks: none
@@ -153,6 +151,52 @@ When ``MongoClient.close()`` is called by any thread, the driver
153151
closes all idle sockets and closes all sockets that are in
154152
use as they are returned to the pool.
155153

154+
155+
How do I prevent the "IllegalArgumentException: Invalid BSON field name" error?
156+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157+
158+
Your application may throw this exception if you pass an incorrectly formatted
159+
document to an operation and you are using a driver version v4.7 or earlier.
160+
161+
.. note::
162+
163+
In driver versions v4.8 and later, this error message was replaced by one
164+
that includes more specific details on what was incorrectly formatted.
165+
166+
For example, the driver throws this error when you call an update operation
167+
and incorrectly omit the update operator as shown in the following code
168+
example:
169+
170+
.. code-block:: java
171+
:emphasize-lines: 4
172+
:copyable: false
173+
174+
// incorrectly formatted update document
175+
collection.updateOne(
176+
new Document().append("name", "fizz"),
177+
new Document().append("name", "buzz")
178+
);
179+
180+
To avoid this error, use the builder class for the appropriate operation.
181+
The driver offers builder classes to create syntactically correct BSON for
182+
MongoDB operations. The prior example can be correctly expressed using
183+
the builder classes as shown in the following code example:
184+
185+
.. code-block:: java
186+
:emphasize-lines: 7
187+
:copyable: false
188+
189+
// Builder class imports
190+
import static com.mongodb.client.model.Filters.*;
191+
import static com.mongodb.client.model.Updates.*;
192+
193+
// ...
194+
195+
collection.updateOne(eq("name", "fizz"), set("name", "buzz"));
196+
197+
To learn more about the available builders classes, see the :ref:`<java-builders>` documentation.
198+
199+
156200
How do I prevent the "IllegalStateException: state should be: open" error?
157201
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158202

@@ -182,6 +226,7 @@ after any code that calls ``close()`` on it.
182226
To learn more about accessing MongoDB from Spring Boot, see
183227
`Spring Boot and MongoDB <https://www.mongodb.com/compatibility/spring-boot>`__.
184228

229+
185230
POJOs
186231
-----
187232

source/fundamentals/builders.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _java-builders:
2+
13
========
24
Builders
35
========

0 commit comments

Comments
 (0)