Why do they use an ObjectId and a Number in the Mongoose Population example?

后端 未结 1 765
-上瘾入骨i
-上瘾入骨i 2021-01-22 19:27

In this example, they create personSchema using ObjectId to reference the Story and this I understand. But then in storySchema

相关标签:
1条回答
  • 2021-01-22 20:04

    Type of reference has to be the same as the referenced schema's _id property.

    In case of personSchema it's a Number.

    storySchema on the other hand, has the _id field assigned automatically by mongoose - it's not specified in parameters for the schema constructor.

    Mongoose assigns each of your schemas an _id field by default if one is not passed into the Schema constructor. The type assiged is an ObjectId to coincide with MongoDBs default behavior

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