Why new documents in mongo have an object and not an ObjectId?

前端 未结 3 2059
青春惊慌失措
青春惊慌失措 2021-01-18 17:08

When inserting new documents in mongodb, ids don\'t look like ObjectId and instead they look like an object.

\"_id\" : {
        \"_bsontype\" : \"ObjectID\"         


        
相关标签:
3条回答
  • 2021-01-18 17:38

    I was having the same issue here: ObjectID not storing hexadecimal value

    It's definitely an issue with environments and something strange with the brew installation of MongoDB. I found that uninstalling from brew and reinstalling manually solved my issue. http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/

    While I didn't figure out from a code/technical standpoint why it is returning the 12-byt BSON ObjectID rather than the Hexadecimal ObjectID... uninstalling MongoDB from brew and reinstalling it manually solved the issue.

    0 讨论(0)
  • 2021-01-18 17:53

    That is what an ObjectID is. It is simply an object that contains those properties.

    http://docs.mongodb.org/manual/reference/object-id/

    ObjectId is a 12-byte BSON type, constructed using:

    • a 4-byte value representing the seconds since the Unix epoch,
    • a 3-byte machine identifier,
    • a 2-byte process id, and
    • a 3-byte counter, starting with a random value.
    {
        "_bsontype" : "ObjectID",
        "id" : "U\u0013[-Ф~\u001d$©t",
        "generationTime" : 1.43439e+09
    }
    

    U\u0013[-Ф~\u001d$©t is the 12 character binary string which gets converted to the familiar 24 char hex string (55107edd8e21f20000fd79a6) when the object as a whole is represented as a text value (i.e. its .toString function is invoked)

    In Mongoose the documents also have a .id getter which give you the 24 char hex as a string value.

    0 讨论(0)
  • 2021-01-18 17:57

    The malformed ObjectIds are caused by a conflict with the mongoose version that mongoose-q is using. You'll need to update to mongoose-q to version 0.1.0. I was using 0.0.17 previously and saw exactly the same behavior that you saw here.

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