0

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? (e.g. 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.

  • Hi, you can check this: https://www.mongodb.com/community/forums/t/lookup-does-data-type-of-join-fields-matter-from-performance-stand-point/127356/5 – NeNaD Jun 30 '22 at 21:50

1 Answers1

1

Yes, it is possible to use types other than ObjectId for refs, but Mongoose recommends using ObjectIds unless you have a good reason to do otherwise.

From the documentation for Populate:

Note: ObjectId, Number, String, and Buffer are valid for use as refs. However, you should use ObjectId unless you are an advanced user and have a good reason for doing so.

Montgomery Watts
  • 3,806
  • 2
  • 10
  • 24