将 DBRef 解析为 Json
Resolve DBRef into Json
我在 MongoDB 中的规范化数据模型结构中遇到以下错误:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.DBRef
是这条线造成的:
System.out.println(document.toJson());
特别是 toJson()
部分。我的文档中有一个 DBRef 对象,因此我可以引用另一个集合中的文档。嵌入式文档结构不是选项。那么我该如何解决这个问题?
你必须导入 DBRef 编解码器才能打印它,如果你想要它在文档 json 样式中你需要为 DBRef 编写你自己的编解码器并将它添加到你给 toJson( ).
例如
CodecRegistry codecRegistry = MongoClientSettings.getDefaultCodecRegistry();
-------
final DocumentCodec codec = new DocumentCodec(codecRegistry, new BsonTypeClassMap());
-------
System.out.println(document.toJson(codec));
因为这是在 google 中搜索错误时的第一个结果,并且接受的答案中的解决方案似乎并不那么简单并且似乎不再有效,因为 MongoClient 没有 getDefaultCodecRegistry()以后,我会 post 对我有帮助的:
protected MongoCollection<Document> collection;
private final CodecRegistry DEFAULT_REGISTRY = CodecRegistries.fromProviders(
asList(new ValueCodecProvider(),
new BsonValueCodecProvider(),
new DocumentCodecProvider(),
new DBRefCodecProvider(),
new DBObjectCodecProvider(),
new BsonValueCodecProvider(),
new GeoJsonCodecProvider(),
new GridFSFileCodecProvider()));
private final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap();
private final DocumentCodec documentCodec = new DocumentCodec(
DEFAULT_REGISTRY,
DEFAULT_BSON_TYPE_CLASS_MAP
);
-----------------------------------------------------------------------
JsonWriterSettings writerSettings = org.bson.json.JsonWriterSettings.
builder().
outputMode(JsonMode.SHELL).
indent(true)
.build();
JsonArray jsonArray = new JsonArray();
collection.
find().
iterator().
forEachRemaining(entry -> jsonArray.add(new JsonParser().parse(entry.toJson(writerSettings, documentCodec)).getAsJsonObject()));
解决方案来自这里:https://github.com/akitoshka/debezium/commit/8dd12d76acced74de7ab184bc18a4384565a70b7
我在 MongoDB 中的规范化数据模型结构中遇到以下错误:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.DBRef
是这条线造成的:
System.out.println(document.toJson());
特别是 toJson()
部分。我的文档中有一个 DBRef 对象,因此我可以引用另一个集合中的文档。嵌入式文档结构不是选项。那么我该如何解决这个问题?
你必须导入 DBRef 编解码器才能打印它,如果你想要它在文档 json 样式中你需要为 DBRef 编写你自己的编解码器并将它添加到你给 toJson( ).
例如
CodecRegistry codecRegistry = MongoClientSettings.getDefaultCodecRegistry();
-------
final DocumentCodec codec = new DocumentCodec(codecRegistry, new BsonTypeClassMap());
-------
System.out.println(document.toJson(codec));
因为这是在 google 中搜索错误时的第一个结果,并且接受的答案中的解决方案似乎并不那么简单并且似乎不再有效,因为 MongoClient 没有 getDefaultCodecRegistry()以后,我会 post 对我有帮助的:
protected MongoCollection<Document> collection;
private final CodecRegistry DEFAULT_REGISTRY = CodecRegistries.fromProviders(
asList(new ValueCodecProvider(),
new BsonValueCodecProvider(),
new DocumentCodecProvider(),
new DBRefCodecProvider(),
new DBObjectCodecProvider(),
new BsonValueCodecProvider(),
new GeoJsonCodecProvider(),
new GridFSFileCodecProvider()));
private final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap();
private final DocumentCodec documentCodec = new DocumentCodec(
DEFAULT_REGISTRY,
DEFAULT_BSON_TYPE_CLASS_MAP
);
-----------------------------------------------------------------------
JsonWriterSettings writerSettings = org.bson.json.JsonWriterSettings.
builder().
outputMode(JsonMode.SHELL).
indent(true)
.build();
JsonArray jsonArray = new JsonArray();
collection.
find().
iterator().
forEachRemaining(entry -> jsonArray.add(new JsonParser().parse(entry.toJson(writerSettings, documentCodec)).getAsJsonObject()));
解决方案来自这里:https://github.com/akitoshka/debezium/commit/8dd12d76acced74de7ab184bc18a4384565a70b7