How could I write a BsonDocument object into a file, and read it again, using Java

丶灬走出姿态 提交于 2019-12-10 20:59:31

问题


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?


回答1:


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.

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.

(Creating BSON object from JSON string)

You can also reference https://api.mongodb.com/java/3.0/org/bson/BsonDocument.html#toJson-org.bson.json.JsonWriterSettings-

Hope this helps you :)



来源:https://stackoverflow.com/questions/38689874/how-could-i-write-a-bsondocument-object-into-a-file-and-read-it-again-using-ja

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!