Invalid BSON field name
15,657
updateOne(Filters.eq("_id", new ObjectId(id)), document);
This is caused by your document format
updateOne({here is condition }, {here is operation})
The document does't have any operation in it ,so it caused this error. You should put it like this:
database.getCollection("user")
.updateOne(Filters.eq("_id", new ObjectId(id)), combine(set( <field1>, <value1>), set(<field2>, <value2> ) ));
Here is the official document
In your case, you can try to use replaceOne() with your code like
collection.replaceOne(eq("item", "paper"),
Document.parse("{ item: 'paper', instock: [ { warehouse: 'A', qty: 60 }, { warehouse: 'B', qty: 40 } ] }"));
Author by
AppDeveloper
Updated on June 04, 2022Comments
-
AppDeveloper almost 2 years
I looked up similar answers to this question but unable to find any.
public void update(String id, String user) { Document document = Document.parse(user); UpdateResult result = database.getCollection("user") .updateOne(Filters.eq("_id", new ObjectId(id)), document); System.out.println(result); }
My JSON payload looks like this.
{ "first": "John", "last": "Doe", "email": "[email protected]", }
Error
Invalid BSON field name first
if I remove first from the payload, it objects on the last and so on.
-
s7vr almost 5 yearsPossible dupe of stackoverflow.com/q/43711716/2683814
-
Vijay about 3 yearsjust use replaceOne instead updateOne
-