{"id":2674,"date":"2021-06-10T08:37:01","date_gmt":"2021-06-10T03:07:01","guid":{"rendered":"https:\/\/explore.techenutia.com\/?p=2674"},"modified":"2021-06-10T08:37:01","modified_gmt":"2021-06-10T03:07:01","slug":"what-is-population-in-mongoose","status":"publish","type":"post","link":"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/","title":{"rendered":"What is Population in Mongoose?"},"content":{"rendered":"\n<p>Population or Query Population in mongoose is a process of automatically replacing paths with other documents from other collections in a single document, multiple documents, objects, multiple objects. That&#8217;s the official definition of the population by a mongoose. But, most of us would understand this one: &#8220;<strong>Population is simply a process of replacing objectID in a mongoose document with its data from its collection in the database<\/strong>. Population can only be done on along with mongoose queries or mongoose objects returned by mongoose queries&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Populate() method<\/h2>\n\n\n\n<p>The process of population is carried out by populate method in mongoose. Now how does it work? Let&#8217;s look at an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ExampleDB.findOne({ title: 'Casino Royale' }).populate('author')<\/code><\/pre>\n\n\n\n<p>In the above line of code <strong>findOne() method will find a document in ExampleDB that has the title Casino Royale and then in that document if there is any id with the key author it will replace that id with the data of the author from Author collection and return the document with author data in it. <\/strong><\/p>\n\n\n\n<p>One thing that confuses beginners: &#8220;<strong>The name of the key that you are populating doesn&#8217;t have to match with the name of the collection of that id<\/strong>&#8220;. For Example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ExampleDB.findOne({title: 'Collection author').populate('authorID')<\/code><\/pre>\n\n\n\n<p>The above code will return the same result as before, why? Because now we have changed the key author in our model to authorID for ExampleDB collection but when we populate authorID MongoDB searches its entire record for that key and populates the data of that key from its collection into this Object and then returns the result. <\/p>\n\n\n\n<p>If you want to nest population i.e. you want to populate author inside ExampleDB and then comments inside author then this is how you do it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ExampleDB\n.findOne({title: \"Nested Population\"})\n.populate({\n   path: \"author\",\n   populate: {\n      path: \"comments\"\n   }\n})<\/code><\/pre>\n\n\n\n<p>These are the most widely used population techniques in mongoose if you wanna dive deeper into the population then read the <a href=\"https:\/\/mongoosejs.com\/docs\/guide.html\">documentation<\/a> here. If the code doesn&#8217;t work or you face any problem feel free to comment I will reach out to you or just drop me an email. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Population or Query Population in mongoose is a process of automatically replacing paths with other documents from other collections in a single document, multiple documents, objects, multiple objects. That&#8217;s the official definition of the population by a mongoose. But, most of us would understand this one: &#8220;Population is simply a&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/\">Read the post<span class=\"screen-reader-text\">What is Population in Mongoose?<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[205],"tags":[778,779,888],"class_list":["post-2674","post","type-post","status-publish","format-standard","hentry","category-code-moments","tag-mongodb","tag-mongoose","tag-populate-mongoose","excerpt","zoom","full-without-featured","even","excerpt-0"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.12 (Yoast SEO v26.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>What is Population in Mongoose? | ManOrInfinity<\/title>\n<meta name=\"description\" content=\"What is query population? How populate method works in mongoDB? What is nested population in mongoose? How to do nested mongoose population\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Population in Mongoose?\" \/>\n<meta property=\"og:url\" content=\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/\" \/>\n<meta property=\"og:site_name\" content=\"ManOrInfinity\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-10T03:07:01+00:00\" \/>\n<meta name=\"author\" content=\"manorinfinity\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@manorinfinity\" \/>\n<meta name=\"twitter:site\" content=\"@manorinfinity\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"manorinfinity\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/\"},\"author\":{\"name\":\"manorinfinity\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901\"},\"headline\":\"What is Population in Mongoose?\",\"datePublished\":\"2021-06-10T03:07:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/\"},\"wordCount\":333,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901\"},\"keywords\":[\"mongodb\",\"mongoose\",\"populate mongoose\"],\"articleSection\":[\"CodeMoments\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/\",\"url\":\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/\",\"name\":\"What is Population in Mongoose? | ManOrInfinity\",\"isPartOf\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/#website\"},\"datePublished\":\"2021-06-10T03:07:01+00:00\",\"description\":\"What is query population? How populate method works in mongoDB? What is nested population in mongoose? How to do nested mongoose population\",\"breadcrumb\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/manorinfinity.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is Population in Mongoose?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/#website\",\"url\":\"https:\/\/manorinfinity.com\/blog\/\",\"name\":\"ManOrInfinity\",\"description\":\"Thrive towards greatness\",\"publisher\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/manorinfinity.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901\",\"name\":\"manorinfinity\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/manorinfinity.com\/wp-content\/uploads\/2023\/06\/moi-logo.png\",\"contentUrl\":\"http:\/\/manorinfinity.com\/wp-content\/uploads\/2023\/06\/moi-logo.png\",\"width\":282,\"height\":260,\"caption\":\"manorinfinity\"},\"logo\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/image\/\"},\"description\":\"Complex Problem Solver, Outloud Thinker, An Outstanding Writer, and a very curious human being\",\"sameAs\":[\"http:\/\/manorinfinity.com\"],\"url\":\"https:\/\/manorinfinity.com\/blog\/author\/manorinfinity\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"What is Population in Mongoose? | ManOrInfinity","description":"What is query population? How populate method works in mongoDB? What is nested population in mongoose? How to do nested mongoose population","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/","og_locale":"en_US","og_type":"article","og_title":"What is Population in Mongoose?","og_url":"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/","og_site_name":"ManOrInfinity","article_published_time":"2021-06-10T03:07:01+00:00","author":"manorinfinity","twitter_card":"summary_large_image","twitter_creator":"@manorinfinity","twitter_site":"@manorinfinity","twitter_misc":{"Written by":"manorinfinity","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/#article","isPartOf":{"@id":"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/"},"author":{"name":"manorinfinity","@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901"},"headline":"What is Population in Mongoose?","datePublished":"2021-06-10T03:07:01+00:00","mainEntityOfPage":{"@id":"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/"},"wordCount":333,"commentCount":0,"publisher":{"@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901"},"keywords":["mongodb","mongoose","populate mongoose"],"articleSection":["CodeMoments"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/","url":"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/","name":"What is Population in Mongoose? | ManOrInfinity","isPartOf":{"@id":"https:\/\/manorinfinity.com\/blog\/#website"},"datePublished":"2021-06-10T03:07:01+00:00","description":"What is query population? How populate method works in mongoDB? What is nested population in mongoose? How to do nested mongoose population","breadcrumb":{"@id":"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/manorinfinity.com\/blog\/2021\/06\/10\/what-is-population-in-mongoose\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/manorinfinity.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What is Population in Mongoose?"}]},{"@type":"WebSite","@id":"https:\/\/manorinfinity.com\/blog\/#website","url":"https:\/\/manorinfinity.com\/blog\/","name":"ManOrInfinity","description":"Thrive towards greatness","publisher":{"@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/manorinfinity.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901","name":"manorinfinity","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/image\/","url":"http:\/\/manorinfinity.com\/wp-content\/uploads\/2023\/06\/moi-logo.png","contentUrl":"http:\/\/manorinfinity.com\/wp-content\/uploads\/2023\/06\/moi-logo.png","width":282,"height":260,"caption":"manorinfinity"},"logo":{"@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/image\/"},"description":"Complex Problem Solver, Outloud Thinker, An Outstanding Writer, and a very curious human being","sameAs":["http:\/\/manorinfinity.com"],"url":"https:\/\/manorinfinity.com\/blog\/author\/manorinfinity\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/posts\/2674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/comments?post=2674"}],"version-history":[{"count":0,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/posts\/2674\/revisions"}],"wp:attachment":[{"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/media?parent=2674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/categories?post=2674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/tags?post=2674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}