Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve DBRef into Json

I'm getting the following error in a Normalized Data Model structure in MongoDB:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.DBRef

It's caused by this line:

System.out.println(document.toJson());

Specifically the toJson() part. I have a DBRef Object in my Document, so I can reference a Document from another Collection. An Embedded Document Structure is not option. So how can I fix this?

like image 937
Georgi Avatar asked Aug 05 '15 08:08

Georgi


1 Answers

You have to import the DBRef Codec for it to print it, if u want it in a document json style you need to write your own Codec for DBRef and add it to the codecregistry you give toJson().

e.g.

CodecRegistry codecRegistry = MongoClientSettings.getDefaultCodecRegistry();
-------
final DocumentCodec codec = new DocumentCodec(codecRegistry, new BsonTypeClassMap());
-------
System.out.println(document.toJson(codec));
like image 116
Georgi Avatar answered Nov 13 '22 03:11

Georgi