_bson ObjectId 转换成 JSON 对象?

_bson ObjectId convert into JSON object?

我试着把它转换成JSON。 _id:对象 _bson类型:"ObjectID" 编号:"X±¸kÍ+I¿9À"

如何转换成JSON格式?

来自https://github.com/mongodb/js-bson

你需要从 BSON 调用反序列化

var doc_2 = bson.deserialize(data);
JSON.stringify(doc_2);

读取那个函数https://github.com/mongodb/js-bson/blob/1.0-branch/extended-json/index.js#L48

您可以期望您的输出为您维持 "type"...

{_id:{"$oid":"58b1bf5bcba40a6a5671620c"}}

如果您真的只想要 OID 的字符串,您可以简单地将字符串覆盖回 _id 键

doc["_id"] = doc["_id"].toString()
JSON.stringify(doc);