MongoDB: BSON to JSON

时光毁灭记忆、已成空白 提交于 2020-01-01 09:55:40

问题


I'm using the native mongoDB driver for node.js. I'd like to get some data from the database and send it as a JSON string via HTTP. Is there a way to convert the BSON to JSON or directly retrieve the data as JSON from the database?

Thanks!


回答1:


In python you can use the simplejson encoder to convert bson to json as follows:

result = db.mycol.find({ ....})
json = simplejson.dumps(result)

It will convert all simple objects, but will have difficulties with others, for example datetime will not work. To deal with this issue the following may work: MongoDB Object Serialized as JSON



来源:https://stackoverflow.com/questions/10439397/mongodb-bson-to-json

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