{"id":17185,"date":"2015-02-13T12:08:49","date_gmt":"2015-02-13T06:38:49","guid":{"rendered":"http:\/\/www.tothenew.com\/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:\/\/www.tothenew.com\/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=\"http:\/\/www.tothenew.com\/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":72},"categories":[1185],"tags":[4843,55,49,1634,4846,1316,1177],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17185"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17185"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17185\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}