From the documentation:
Mongoose assigns each of your schemas an id virtual getter by default
which returns the documents _id field cast to a string, or in the case
of ObjectIds, its hexString.
So, basically, the id
getter returns a string representation of the document's _id
(which is added to all MongoDB documents by default and have a default type of ObjectId
).
Regarding what's better for referencing, that depends entirely on the context (i.e., do you want an ObjectId
or a string
). For example, if comparing id
's, the string is probably better, as ObjectId
's won't pass an equality test unless they are the same instance (regardless of what value they represent).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…