MongoDB native: is there any difference between toString and toHexString methods?
Asked Answered
S

1

15

I'm using Node.js v0.12.0 with MongoDB driver v1.4.34. So, is there any difference between converting ObjectID to String with toString and toHexString methods?

Stenophyllous answered 13/3, 2015 at 11:5 Comment(0)
C
18

toHexString method returns the ObjectID id as a 24 byte hex string representation.

// Create a new ObjectID
var objectId = new ObjectID();
// Verify that the hex string is 24 characters long
assert.equal(24, objectId.toHexString().length);

You won't need to base64 encode the result of calling toString on an ObjectId as it's returned as a hex number already. You could also call: _id.toHexString() to get the hex value directly.
Click this link to see MongoDB source (toString just wraps toHexString).

Crackerjack answered 13/3, 2015 at 11:10 Comment(1)
The last line in this answer is the most important here. The short answer is: No, there is no difference.Pallium

© 2022 - 2024 — McMap. All rights reserved.