我使用Node.js v0.12.0和MongoDB驱动程序v1.4.34。那么,使用toString和toHexString方法将ObjectID转换为String之间有什么区别吗?
答案 0 :(得分:7)
toHexString
方法将ObjectID id作为24字节十六进制字符串表示返回。
// Create a new ObjectID
var objectId = new ObjectID();
// Verify that the hex string is 24 characters long
assert.equal(24, objectId.toHexString().length);
您不需要在toString
上对ObjectId
调用_id.toHexString()
的结果进行base64编码,因为它已经作为十六进制数返回。您也可以调用:{{1}}直接获取十六进制值。
点击此链接查看MongoDB源(toString just wraps toHexString)。