what does populate in mongoose mean?

后端 未结 4 979
耶瑟儿~
耶瑟儿~ 2020-12-12 21:51

I came across the following line of code which I couldn\'t understand ,although there are lot of tutorials that gives information related to examples of populate

4条回答
  •  生来不讨喜
    2020-12-12 22:08

    populate() function in mongoose is used for populating the data inside the reference. In your example StorySchema is having _creator field which will reference to the _id field which is basically the ObjectId of the mongodb document.

    populate() function can accept a string or an object as an input.

    Where string is the field name which is required to be populated. In your case that is _creator. After mongoose found one doc from mongodb and the result of that is like below

    _creator: {
      name: "SomeName",
      age: SomeNumber,
      stories: [Set Of ObjectIDs of documents in stories collection in mongodb]
    },
    title: "SomeTitle",
    fans: [Set of ObjectIDs of documents in persons collection in mongodb]
    

    populate can also accept the object as an input.

    You can find the documents of mongoose's populate() function here : http://mongoosejs.com/docs/2.7.x/docs/populate.html or https://mongoosejs.com/docs/populate.html

提交回复
热议问题