Reputation: 2008
I'm currently learning MongoDB why do we use .populate()
. What is the difference between .populate()
and .virtual()
?
Can anyone tell me how they differ and when one should be used over the other?
Upvotes: 1
Views: 527
Reputation: 1688
Both serves a different purpose and one cannot be a replacement of other.
Virtual: If you want attributes that you can get and set but that are not themselves persisted to mongodb, virtual attributes is the Mongoose feature for you.Remember, only non-virtual properties work as part of queries and for field selection.See Virtual
Populate: ObjectIds can refer to another document in a collection within our database and be populate()d when querying.It gives you DBRef-like behavior. See Populate
Upvotes: 2