Convert ObjectID (Mongodb) to String in JavaScript

后端 未结 16 1252
青春惊慌失措
青春惊慌失措 2020-12-02 19:50

I want to convert ObjectID (Mongodb) to String in JavaScript. When I get a Object form MongoDB. it like as a object has: timestamp, second, inc, machine. I can\'t convert to

相关标签:
16条回答
  • 2020-12-02 20:44

    Use toString: var stringId = objectId.toString()

    Works with the latest Node MongoDB Native driver (v3.0+):

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

    0 讨论(0)
  • 2020-12-02 20:44

    You can use string formatting.

    const stringId = `${objectId}`;

    0 讨论(0)
  • 2020-12-02 20:45

    Try this:

    objectId.str
    

    See the doc.

    ObjectId() has the following attribute and methods:

    [...]

    • str - Returns the hexadecimal string representation of the object.
    0 讨论(0)
  • 2020-12-02 20:46

    Just use this : _id.$oid

    And you get the ObjectId string. This come with the object.

    0 讨论(0)
提交回复
热议问题