what does populate in mongoose mean?

后端 未结 4 961
耶瑟儿~
耶瑟儿~ 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

    0 讨论(0)
  • 2020-12-12 22:08

    Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s). We may populate a single document, multiple documents, a plain object, multiple plain objects, or all objects returned from a query. Let's look at some examples.

    Let me explain so if you are making a blog post and you want to add an option where the content reader can know the person who wrote the blog so you can use it in the following way:

    Blog.findOne({title:a random blog})
    

    and in the other hand if you want to return the id and the info of the blogger who wrote the blog you can do it like:

    Blog.findOne({title:a random blog}).populate(blogger)
    

    Well you can use it in many ways this is just an example of it.

    0 讨论(0)
  • 2020-12-12 22:09

    .populate() is being used in order to bring only needed information.

    EXAMPLE without .populate()

    User.findOne({ name: Bob })
    

    Will return

    {
      Bob : {
        _id : dasd348ew,
        email: bob@example.com,
        age: 25,
        job: teacher,
        nationality: American,
      }
    }
    

    EXAMPLE with .populate()

    User.findOne({ name: Bob }).populate("Bob", "job email")
    

    Will return

    {
      Bob : {
        job: teacher,
        email: bob@example.com,
      }
    }
    
    0 讨论(0)
  • 2020-12-12 22:18

    I came across that question randomly, but I feel I need to help here, even if it's old because I'm not convinced by the way it is explained :

    Populate() function populate...

    That might be very clear for natives English speaker, but maybe not for others.

    In short

    Populate will automatically replace the specified path in the document, with document(s) from other collection(s).

    Long version

    Let's take your example:

    Story.findOne({ title: Nintendo })
    

    Will return a Story of that kind :

    {
      _creator : A0jfdSMmEJj9, //id of the creator (totally random, just for a clear example)
        title    : Nintendo,
        fans     : [r432i900fds09809n, fdsjifdsjfueu88] // again, totally random that I've typed here
      }
    }
    

    In some case, those kind of request would be enough, because we don't care about the author or the fans so, having some ID won't bother us much.

    But in the case where i need that _creator's name, I'll need to make another request to find it in database. Except, that here in mongoose we have a clever function called populate() that we can chained to our previous request in order to directly get that information in our answer without explictly doing an additional request.

    Story.findOne({ title: Nintendo }).populate('_creator')
    

    will return

    {
      _creator : {
           _id : A0jfdSMmEJj*9,
           name: Sai,
           age: 100,
           stories : [fdsfdsfdsew38u, 89hr3232, ...]
        },
        title    : Nintendo,
        fans     : [r432i900fds09809n, fdsjifdsjfueu88]
      }
    }
    

    But maybe, that's too much information, and we don't want the stories that he wrote and his age and name are enough. Populate can then take an other argument containing the field that we need

    Story.findOne({ title: Nintendo }).populate('_creator', 'name age')
    

    result ==>

    {
      _creator : {
           name: Sai,
           age: 100,
        },
        title    : Nintendo,
        fans     : [r432i900fds09809n, fdsjifdsjfueu88]
      }
    }
    
    0 讨论(0)
提交回复
热议问题