[JAVA-2513] org.bson.codecs.configuration.CodecConfigurationException in when update document with specified array field Created: 08/May/17  Updated: 27/Oct/23  Resolved: 22/Jun/17

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: 3.4.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Ehsan Farahani Asil Assignee: Ross Lawley
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

i want to update userinfo.languages filled in this document.
{"userInfo": {
"fullName": "Ehsan Farahani Asil",
"birthdate": 0,
"aboutme": "Nothing",
"interests": [],
"drinking": "",
"smoking": "",
"languages": [

{ "name": "English", "level": "Native" }

,

{ "name": "Persian", "level": "Native" }

],
"relationship": "",
"sexuality": "",
"bodyType": "",
"eyeColor": "",
"hairColor": "Black",
"living": "",
"kids": "No",
"height": 0,
"weight": 0,
"work": "Software Developer"
}
}
when i want to update this document with this code, mongodob java driver return error org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class project.models.base.Language.

i use this code for update
UpdateResult updateResult = mongoCollection.updateOne(eq("_id", new ObjectId(usersId)), Updates.set("userInfo.languages",userInfo.getLanguages()));

public class UserInfo {
private List<Language> languages = null;
public List<Language> getLanguages()

{ return languages; }

public void setLanguages(List<Language> languages)

{ this.languages = languages; }

}

public class Language {
@SerializedName("name")
private String name;
@SerializedName("level")
private String level;

public String getName()

{ return name; }

public void setName(String name)

{ this.name = name; }

public String getLevel()

{ return level; }

public void setLevel(String level)

{ this.level = level; }

}

i problem with update each field that like languages use list<> or arrayList. in other filed that use integer, Boolean, string, double i don't have any problems.

Best Regards
Ehsan Farahani Asil



 Comments   
Comment by Ehsan Farahani Asil [ 09/May/17 ]

thanks very much. i solve this problem with this action. first get document and then updated specified changed field.

Best Regards
Ehsan Farahani Asil

Comment by Ross Lawley [ 08/May/17 ]

Hi ehsansis0,

The error is because the Mongo Java Driver doesn't have a Codec for the Language object and therefore doesn't know how to convert it into BSON. The next release (3.5) will provide a POJO Codec which will allow you to easily register POJO's instances into the Codec Registry. Until then you either must handwrite a Codec or convert to a Document manually and then update with that object.

I hope that helps,

Ross

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