我想在JavaScript中将ObjectID (Mongodb)转换为字符串。当我从MongoDB获取一个对象时。它就像一个对象一样有:时间戳,秒,公司,机器。我不能转换为字符串。
发布于 2013-05-10 17:22:42
以下是将ObjectId
转换为字符串的工作示例
> a=db.dfgfdgdfg.findOne()
{ "_id" : ObjectId("518cbb1389da79d3a25453f9"), "d" : 1 }
> a['_id']
ObjectId("518cbb1389da79d3a25453f9")
> a['_id'].toString // This line shows you what the prototype does
function () {
return "ObjectId(" + tojson(this.str) + ")";
}
> a['_id'].str // Access the property directly
518cbb1389da79d3a25453f9
> a['_id'].toString()
ObjectId("518cbb1389da79d3a25453f9") // Shows the object syntax in string form
> ""+a['_id']
518cbb1389da79d3a25453f9 // Gives the hex string
我尝试了其他各种功能,如toHexString()
,但没有成功。
发布于 2016-06-23 00:12:38
在shell中
ObjectId("507f191e810c19729de860ea").str
在js using the native driver for node中
objectId.toHexString()
发布于 2018-06-27 04:10:24
使用toString:var stringId = objectId.toString()
与最新的节点MongoDB本机驱动程序(v3.0+)配合使用:
https://stackoverflow.com/questions/16478552
复制相似问题