I would like to ask a question in this post but I don't have the need reputation to ask. So I created this post. The question is: Is possible to referencing other schema in mongoose using another variable type? (eg Number, String, etc). I know you need the ref flag in specification but I don't know if mongoose is limited to reference by only using Schema.Types.ObjectId type.
Any clarification will be greatly recieved
Thanks.
Yes, it is possible to use types other than ObjectId
for ref
s, but Mongoose recommends using ObjectId
s unless you have a good reason to do otherwise.
From the documentation for Populate :
Note :
ObjectId
,Number
,String
, andBuffer
are valid for use as refs. However, you should useObjectId
unless you are an advanced user and have a good reason for doing so.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.