{"id":17185,"date":"2015-02-13T12:08:49","date_gmt":"2015-02-13T06:38:49","guid":{"rendered":"https:\/\/2thenew.online\/blog\/?p=17185"},"modified":"2016-08-26T17:37:06","modified_gmt":"2016-08-26T12:07:06","slug":"high-performance-find-query-using-lean-in-mongoose-2","status":"publish","type":"post","link":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/","title":{"rendered":"High performance find query using lean() in mongoose"},"content":{"rendered":"<p>Lets start with a basic <strong>find<\/strong> query in mongoose and observe its execution time.<\/p>\n<p>[js]<br \/>\n Event.find({&#8216;schedule.closing&#8217;: {&#8216;$gte&#8217;: +new Date()}},<br \/>\n        {},<br \/>\n        {limit:20, skip:0}<br \/>\n    ).exec(function (err, events){<br \/>\n &#8230;&#8230;..<br \/>\n &#8230;&#8230;..<br \/>\n  }<br \/>\n);<br \/>\n[\/js]<\/p>\n<p>In my last <a title=\"Nodejs development\" href=\"https:\/\/2thenew.online\/mean-node-js-development-consulting\">project on Nodejs<\/a>, I have used the above query to fetch the events from <strong>&#8220;Event&#8221;<\/strong> collection satisfying the given condition. After computing the average execution time, it came out to be <strong>218ms<\/strong>.<\/p>\n<p>Then, I applied <strong>lean()<\/strong> in the same query:<\/p>\n<p>[js]<br \/>\n Event.find({&#8216;schedule.closing&#8217;: {&#8216;$gte&#8217;: +new Date()}},<br \/>\n        {},<br \/>\n        {limit:20, skip:0}<br \/>\n    ).lean().exec(function (err, events){<br \/>\n &#8230;&#8230;..<br \/>\n &#8230;&#8230;..<br \/>\n  }<br \/>\n);<br \/>\n[\/js]<\/p>\n<p>This time the average execution time came out to be <strong>71ms<\/strong> which is drastically less than the without &#8220;lean&#8221; version of the same query.<\/p>\n<p>So, you must be wondering what exactly lean() did here?<\/p>\n<p>As described by the mongoose docs, documents returned from queries with the lean option true are plain javascript objects, not Mongoose Documents. They have no save method, getters\/setters or other Mongoose magic applied. So in this way the over head attached to the mongoose document is not there in case of lean and we get high performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using lean() in mongoose provides high performance queries.<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":103,"footnotes":""},"categories":[1185],"tags":[4843,55,49,1634,4846,1316,1177],"class_list":["post-17185","post","type-post","status-publish","format-standard","hentry","category-node-js-2","tag-database","tag-javascript","tag-json","tag-lean","tag-mongodb","tag-mongoose","tag-nodejs-2"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Using lean() in mongoose provides high performance queries.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Sakshi Tyagi\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"High performance find query using lean() in mongoose | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Using lean() in mongoose provides high performance queries.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"High performance find query using lean() in mongoose | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Using lean() in mongoose provides high performance queries.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/high-performance-find-query-using-lean-in-mongoose-2\\\/#article\",\"name\":\"High performance find query using lean() in mongoose | TO THE NEW Blog\",\"headline\":\"High performance find query using lean() in mongoose\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2015-02-13T12:08:49+05:30\",\"dateModified\":\"2016-08-26T17:37:06+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":11,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/high-performance-find-query-using-lean-in-mongoose-2\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/high-performance-find-query-using-lean-in-mongoose-2\\\/#webpage\"},\"articleSection\":\"Node.js, Database, javascript, JSON, lean, MongoDB, Mongoose, nodejs\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/high-performance-find-query-using-lean-in-mongoose-2\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"position\":2,\"name\":\"Node.js\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/high-performance-find-query-using-lean-in-mongoose-2\\\/#listItem\",\"name\":\"High performance find query using lean() in mongoose\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/high-performance-find-query-using-lean-in-mongoose-2\\\/#listItem\",\"position\":3,\"name\":\"High performance find query using lean() in mongoose\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/\",\"name\":\"Sakshi Tyagi\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/high-performance-find-query-using-lean-in-mongoose-2\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Sakshi Tyagi\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/high-performance-find-query-using-lean-in-mongoose-2\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/high-performance-find-query-using-lean-in-mongoose-2\\\/\",\"name\":\"High performance find query using lean() in mongoose | TO THE NEW Blog\",\"description\":\"Using lean() in mongoose provides high performance queries.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/high-performance-find-query-using-lean-in-mongoose-2\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"datePublished\":\"2015-02-13T12:08:49+05:30\",\"dateModified\":\"2016-08-26T17:37:06+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"High performance find query using lean() in mongoose | TO THE NEW Blog","description":"Using lean() in mongoose provides high performance queries.","canonical_url":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/#article","name":"High performance find query using lean() in mongoose | TO THE NEW Blog","headline":"High performance find query using lean() in mongoose","author":{"@id":"https:\/\/2thenew.online\/blog\/author\/sakshi\/#author"},"publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"},"datePublished":"2015-02-13T12:08:49+05:30","dateModified":"2016-08-26T17:37:06+05:30","inLanguage":"en-US","commentCount":11,"mainEntityOfPage":{"@id":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/#webpage"},"articleSection":"Node.js, Database, javascript, JSON, lean, MongoDB, Mongoose, nodejs"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.online\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/node-js-2\/#listItem","position":2,"name":"Node.js","item":"https:\/\/2thenew.online\/blog\/category\/node-js-2\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/#listItem","name":"High performance find query using lean() in mongoose"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/#listItem","position":3,"name":"High performance find query using lean() in mongoose","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.online\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.online\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.online\/blog\/author\/sakshi\/#author","url":"https:\/\/2thenew.online\/blog\/author\/sakshi\/","name":"Sakshi Tyagi","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g","width":96,"height":96,"caption":"Sakshi Tyagi"}},{"@type":"WebPage","@id":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/#webpage","url":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/","name":"High performance find query using lean() in mongoose | TO THE NEW Blog","description":"Using lean() in mongoose provides high performance queries.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.online\/blog\/author\/sakshi\/#author"},"creator":{"@id":"https:\/\/2thenew.online\/blog\/author\/sakshi\/#author"},"datePublished":"2015-02-13T12:08:49+05:30","dateModified":"2016-08-26T17:37:06+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.online\/blog\/#website","url":"https:\/\/2thenew.online\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"High performance find query using lean() in mongoose | TO THE NEW Blog","og:description":"Using lean() in mongoose provides high performance queries.","og:url":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/","og:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"High performance find query using lean() in mongoose | TO THE NEW Blog","twitter:description":"Using lean() in mongoose provides high performance queries.","twitter:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"17185","title":"High performance find query using lean() in mongoose | #site_title","description":"Using lean() in mongoose provides high performance queries.","keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 12:51:20","updated":"2024-02-29 08:41:23","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\/category\/node-js-2\/\" title=\"Node.js\">Node.js<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tHigh performance find query using lean() in mongoose\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.online\/blog"},{"label":"Node.js","link":"https:\/\/2thenew.online\/blog\/category\/node-js-2\/"},{"label":"High performance find query using lean() in mongoose","link":"https:\/\/2thenew.online\/blog\/high-performance-find-query-using-lean-in-mongoose-2\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/17185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/comments?post=17185"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/17185\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/media?parent=17185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/categories?post=17185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/tags?post=17185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}