Skip to content

Commit 44e8a01

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 54c537b commit 44e8a01

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
@@ -4,8 +4,6 @@
44
FAQ
55
===
66

7-
.. default-domain:: mongodb
8-
97
.. contents:: On this page
108
:local:
119
:backlinks: none
@@ -148,6 +146,52 @@ When ``MongoClient.close()`` is called by any thread, the driver
148146
closes all idle sockets and closes all sockets that are in
149147
use as they are returned to the pool.
150148

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

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

224+
180225
POJOs
181226
-----
182227

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)