what does populate in mongoose mean?
Asked Answered
F

6

59

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 but there is none that explains what exactly it means.Here is a example

var mongoose = require('mongoose'), Schema = mongoose.Schema

var PersonSchema = new Schema({
  name    : String,
  age     : Number,
  stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});

var StorySchema = new Schema({
  _creator : {
     type: Schema.ObjectId,
     ref: 'Person'
  },
  title    : String,
  fans     : [{ type: Schema.ObjectId, ref: 'Person' }]
});

var Story  = mongoose.model('Story', StorySchema);
var Person = mongoose.model('Person', PersonSchema);
Story.findOne({ title: /Nintendo/i }).populate('_creator') .exec(function (err, story) {
if (err) ..
  console.log('The creator is %s', story._creator.name);
  // prints "The creator is Aaron"
})
Footwork answered 27/6, 2016 at 10:46 Comment(2)
Populate is generally used to populate a ref objects property when you are querying mongoosejs.com/docs/2.7.x/docs/populate.htmlKimble
I didn't find any blog better than this one: medium.com/@nicknauert/mongooses-model-populate-b844ae6d1ee7Weekday
F
26

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

Frampton answered 1/7, 2016 at 8:39 Comment(1)
Seems like it's a sort of auto-join (JOIN a la SQL). Right?Plafond
C
178

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

Your example

Story.findOne({ title: Nintendo })

will return a Story of this kind:

{
  _creator : A0jfdSMmEJj9, // example value
    title    : Nintendo,
    fans     : [r432i900fds09809n, fdsjifdsjfueu88] // example values
  }
}

That kind of request would be enough when we don't care about the author or the fans, only their IDs.

But in the case where we need that _creator's name, we would need to make another request to find it in the database.

In Mongoose we have function populate() that we can chain to our previous request to directly get that information in our answer without 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]
  }
}

If that's too much information, and his age and name are enough but we don't want the stories that he wrote, populate can take another argument containing the field that we need.

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

will return

{
  _creator : {
       name: Sai,
       age: 100,
    },
    title    : Nintendo,
    fans     : [r432i900fds09809n, fdsjifdsjfueu88]
  }
}
Christinchristina answered 26/10, 2018 at 5:55 Comment(4)
if a user had published n number of books then how can I return all the documents?Xanthic
could someone explain me what does this "-__v" mean inside populate? .populate("roles", "-__v") I can provide more info if needed, but I feel like this is something obvious but i don't know it.Uriia
Wait..if we don't have to make another request then how is it different then MySQL database? I thought we cannot retrieve relational data in one request in noSQL databases and that's why we do data denormalisation????Padding
@AliAkber - In MongoDB the __v attribute is the version key, and it has a number as its value that represents a document's version number. It's to help with data integrity in case of data inconsistencies and version conflicts. Check out this answer for more info: stackoverflow.com/a/12495922Cyclonite
F
26

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

Frampton answered 1/7, 2016 at 8:39 Comment(1)
Seems like it's a sort of auto-join (JOIN a la SQL). Right?Plafond
L
14

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

EXAMPLE without .populate()

User.findOne({ name: Bob })

Will return

{
  Bob : {
    _id : dasd348ew,
    email: [email protected],
    age: 25,
    job: teacher,
    nationality: American,
  }
}

EXAMPLE with .populate()

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

Will return

{
  Bob : {
    job: teacher,
    email: [email protected],
  }
}
Lindholm answered 13/11, 2020 at 7:32 Comment(0)
M
8

This answer to this question is not complete if you need to populate across multiple levels. We can use it to get a list of a user's friends. But to retrieve friends of a user's friends we can do this:

const userSchema = new Schema({
  name: String,
  friends: [{ type: ObjectId, ref: 'User' }]
});

User.
  findOne({ name: 'Val' }).
  populate({
    path: 'friends',
    // Get friends of friends - populate the 'friends' array for every friend
    populate: { path: 'friends' }
  });
Manaus answered 10/4, 2021 at 12:45 Comment(0)
W
0

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.

Wexler answered 4/10, 2020 at 4:46 Comment(0)
F
0

Example:

const allMessages = asyncHandler(async (req, res) => {
  try {
    const messages = await Message.find({ chat: req.params.chatId })
      .populate("sender", "name pic email")
      .populate("chat");
    res.json(messages);
  } catch (error) {
    res.status(400);
    throw new Error(error.message);
  }
});

The populate() method is called on the query, which is used to automatically load the data of the sender and chat field into the messages results. The first argument to populate is the name of the the field that we want to load, and the second argument is the fields of the related collection that we want to select.

populate("sender", "name pic email") will load the data of sender field with the name, pic, and email fields of the related User collection.

populate("chat") will load the data of the chat field with all the fields of the related chat collection.

Fluoroscopy answered 10/1, 2023 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.