Hi, Talents, I need your help! #14392
-
Is there any performance difference (process time of query) between using Mongoose population and direct object inclusion? When should each be used ? Mongoose population example:
Mongoose object nesting example:
Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi The first thing to understand about mongoose population is that it is not magic, but just a convenience method that allows you to retrieve related information without doing it all yourself. The concept is essentially for use where you decide you are going to need to place data in a separate collection rather than embedding that data, and your main considerations should be typically on document size or where that related information is subject to frequent updates that would make maintaining embedded data unwieldy. The "not magic" part is that essentially what happens under the covers is that when you "reference" another source, the populate function makes an additional query/queries to that "related" collection in order to "merge" those results of the parent object that you have retrieved. You could do this yourself, but the method is there for convenience to simplify the task. The obvious "performance" consideration is that there is not a single round trip to the database (MongoDB instance) in order to retrieve all the information. There is always more than one. As a sample, take two collections:
And the items:
The "best" that can be done by a "referenced" model or the use of populate (under the hood) is this:
So there are clearly "at least" two queries and operations in order to "join" that data. The embedding concept is essentially the MongoDB answer to how to deal with not supporting "joins"1. So that rather that split data into normalized collections you try to embed the "related" data directly within the document that uses it. The advantages here are that there is a single "read" operation for retrieving the "related" information, and also a single point of "write" operations to both update "parent" and "child" entries, though often not possible to write to "many" children at once without processing "lists" on the client or otherwise accepting "multiple" write operations, and preferably in "batch" processing. Data then rather looks like this ( compared to the example above ):
Therefore actually fetching the data is just a matter of:
Hope it can be helpful to you! |
Beta Was this translation helpful? Give feedback.
Hi
The first thing to understand about mongoose population is that it is not magic, but just a convenience method that allows you to retrieve related information without doing it all yourself.
The concept is essentially for use where you decide you are going to need to place data in a separate collection rather than embedding that data, and your main considerations should be typically on document size or where that related information is subject to frequent updates that would make maintaining embedded data unwieldy.
The "not magic" part is that essentially what happens under the covers is that when you "reference" another source, the populate function makes an additional query/queries to …