如何使用Java将BsonDocument对象写入文件,然后再次读取 [英] How could I write a BsonDocument object into a file, and read it again, using Java

查看:510
本文介绍了如何使用Java将BsonDocument对象写入文件,然后再次读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将BsonDocument输出到文件系统而不是mongodb,然后使用Java再次读取它。但是当前的BasicBSONEncoder仅支持旧版本的BSONObject,如何将BsonDocument编码为二进制文件?
还是有什么方法可以将BsonDocument转换为BSONObject?

I want to output a BsonDocument to a file system, not to mongodb, and read it again using java. But current BasicBSONEncoder only support BSONObject which is the old version.How can I encode BsonDocument into binary? Or is there any way I can convert BsonDocument into BSONObject?

推荐答案

最简单的方法是将BSON转换为JSON并使用java.io API将这些数据写入文件。相反的是使用Reader API的(BufferedReader或FileReader)将其读回并转换为BSON。

The eaziest is to convert the BSON to JSON and use java.io API's to write these data to files. The reverse would be to use Reader API's (BufferedReader or FileReader) to read these back and convert to BSON.

现在要将BSON转换为JSON,有多种方法,具体取决于官方Mongo Driver上使用的实用程序以及API,您可以选择自己喜欢的一种。反之亦然,该线程上有很多选项。

Now to convert BSON to JSON there are multiple ways , depends on utilities used as well as API's on the official Mongo Driver you could choose one of your liking. The reverse is also true , a lot of options are on this thread.

根据JSON字符串创建BSON对象

您还可以引用
https://api.mongodb.com/java /3.0/org/bson/BsonDocument.html#toJson-org.bson.json.JsonWriterSettings-

希望这对您有帮助:)

这篇关于如何使用Java将BsonDocument对象写入文件,然后再次读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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