MongoDB "NumberLong/$numberLong" issue while converting back to Java Object MongoDB "NumberLong/$numberLong" issue while converting back to Java Object json json

MongoDB "NumberLong/$numberLong" issue while converting back to Java Object


We can use below code:

Document doc = documentCursor.next();JsonWriterSettings relaxed = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();CustomeObject obj = gson.fromJson(doc.toJson(relaxed), CustomeObject.class);


Mongo db uses Bson format with its own types which follows json standards but it can't be parsed by json library without writing the custom wrapper/codec.

You can use third party framework/plugins to make the library take care of converting between document and pojo.

If that is not an option for you, you will have to do mapping yourself.

Document mongoDocument = mycollection.find(searchCondition);Model model= new Model();model.setProperty(mongoDocument.get("property");


Take a look at: converting Document objects in MongoDB 3 to POJOS

I had the same problem. The workaround with com.mongodb.util.JSON.serialize(document) does the trick.