MongoDB:BSON 到 JSON

问题描述 投票:0回答:1

我正在为 node.js 使用本机 mongoDB 驱动程序。 我想从数据库中获取一些数据并通过 HTTP 将其作为 JSON 字符串发送。 有没有办法将 BSON 转换为 JSON 或直接从数据库中检索数据作为 JSON?

谢谢!

json node.js mongodb bson
1个回答
0
投票

在 python 中你可以使用 simplejson 编码器将 bson 转换为 json,如下所示:

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

它将转换所有简单的对象,但对其他对象会有困难,例如 datetime 将不起作用。为了解决这个问题,以下方法可能有效:MongoDB Object Serialized as JSON

© www.soinside.com 2019 - 2024. All rights reserved.