将DBRef解析为Json [英] Resolve DBRef into Json

查看:572
本文介绍了将DBRef解析为Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB中的规范化数据模型结构中收到以下错误:

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

这是由此行引起的:

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

特别是 toJson()部分。我的文档中有一个DBRef对象,所以我可以从另一个Collection中引用一个Document。嵌入式文档结构不是选项。那么我该如何解决这个问题呢?

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?

推荐答案

你必须导入DBRef Codec才能打印它,如果你想要的话一个文档json样式你需要为DBRef编写自己的Codec并将它添加到你给json()的codecregistry。

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().

例如

CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry());
-------
final DocumentCodec codec = new DocumentCodec(codecRegistry, new BsonTypeClassMap());
-------
System.out.println(document.toJson(codec));

这篇关于将DBRef解析为Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆