Difference between “id” and “_id” fields in MongoDB

前端 未结 4 1225
南笙
南笙 2020-11-30 02:01

Is there any difference between using the field ID or _ID from a MongoDB document?

I am asking this, because I usually use \"_id\", however I saw this sort({id:-1})

4条回答
  •  暖寄归人
    2020-11-30 02:59

    My two cents:

    The _id field

    MongoDB assigns an _id field to each document and assigns primary index on it. There're ways by which we can apply secondary indices as well. By default, MongoDB creates values for the _id field of type ObjectID. This value is defined in BSON spec and it's structured this way:

    ObjectID (12 bytes HEX string) = Date (4 bytes, a timestamp value representing number of seconds since the Unix epoch) + MAC address (3 bytes) + PID (2 bytes) + Counter (3 bytes)

提交回复
热议问题