首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在JavaScript中将ObjectID (Mongodb)转换为字符串

在JavaScript中将ObjectID (Mongodb)转换为字符串
EN

Stack Overflow用户
提问于 2013-05-10 16:58:12
回答 16查看 98.2K关注 0票数 75

我想在JavaScript中将ObjectID (Mongodb)转换为字符串。当我从MongoDB获取一个对象时。它就像一个对象一样有:时间戳,秒,公司,机器。我不能转换为字符串。

EN

回答 16

Stack Overflow用户

发布于 2013-05-10 17:22:42

以下是将ObjectId转换为字符串的工作示例

代码语言:javascript
复制
> 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(),但没有成功。

票数 25
EN

Stack Overflow用户

发布于 2016-06-23 00:12:38

shell

ObjectId("507f191e810c19729de860ea").str

在js using the native driver for node

objectId.toHexString()

票数 19
EN

Stack Overflow用户

发布于 2018-06-27 04:10:24

使用toString:var stringId = objectId.toString()

与最新的节点MongoDB本机驱动程序(v3.0+)配合使用:

http://mongodb.github.io/node-mongodb-native/3.0/

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16478552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档