{"id":175,"date":"2020-12-16T08:15:30","date_gmt":"2020-12-16T08:15:30","guid":{"rendered":"http:\/\/santosh-shah.com\/?p=175"},"modified":"2020-12-16T08:15:30","modified_gmt":"2020-12-16T08:15:30","slug":"nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit","status":"publish","type":"post","link":"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/","title":{"rendered":"NodeJs mongodb query to fetch collection data with total length offset and limit"},"content":{"rendered":"\n<p>This is how we query from mongodb collection to fetch data with total length, offset and limit. For total length we need to use async.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let url = \"mongodb+srv:\/\/demodbuser:demodbpassword@myuser.zfebp.mongodb.net\/demodb?retryWrites=true&amp;w=majority\";\n\n\tmongoose.set(\"useNewUrlParser\", true);\n\tmongoose.set(\"useUnifiedTopology\", true);\n\tmongoose.connect( url, (err, database) => { \n\t\tif(err) {\n\t\t\treturn console.error(err);\n\t\t}\n\t\tdb = database;\n\n\t});\n\n\nexports.getAllMinisters = async (req, res, next)=>{\n\ttotalCount  = await db.collection('allMinisters').countDocuments({});\n\tdb.collection('allMinisters').find({}).skip(0).limit(10).toArray(function (err, docs) {\n\t\tif (err) {return next(err);}\n\t\t\tconst response = {\n\t\t\t\ttotalCount: totalCount,\n\t\t\t\tlength: docs.length,\n\t\t\t\tallMinisters: docs.map(docs=>docs),\n\t\t\t\tstatus: 'success'\n\t\t\t}\n\t\tres.statusCode = 200;\n\t\tres.setHeader('Content-Type', 'application\/json');\n\t\tres.end(JSON.stringify(response));\n\t});\n};\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is how we query from mongodb collection to fetch data with total length, offset and limit. For total length we need to use async.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>NodeJs mongodb query to fetch collection data with total length offset and limit - Santosh Kumar Shah<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NodeJs mongodb query to fetch collection data with total length offset and limit - Santosh Kumar Shah\" \/>\n<meta property=\"og:description\" content=\"This is how we query from mongodb collection to fetch data with total length, offset and limit. For total length we need to use async.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/\" \/>\n<meta property=\"og:site_name\" content=\"Santosh Kumar Shah\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-16T08:15:30+00:00\" \/>\n<meta name=\"author\" content=\"Santosh Kumar Shah\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Santosh Kumar Shah\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/\",\"url\":\"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/\",\"name\":\"NodeJs mongodb query to fetch collection data with total length offset and limit - Santosh Kumar Shah\",\"isPartOf\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/#website\"},\"datePublished\":\"2020-12-16T08:15:30+00:00\",\"author\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f\"},\"breadcrumb\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/santosh-shah.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"NodeJs mongodb query to fetch collection data with total length offset and limit\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#website\",\"url\":\"https:\/\/santosh-shah.com\/blog\/\",\"name\":\"Santosh Kumar Shah\",\"description\":\"JavaScript Developer\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/santosh-shah.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f\",\"name\":\"Santosh Kumar Shah\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g\",\"caption\":\"Santosh Kumar Shah\"},\"description\":\"I am JavaScript developer.\",\"sameAs\":[\"https:\/\/santosh-shah.com\/blog\"],\"url\":\"https:\/\/santosh-shah.com\/blog\/author\/sks7yu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"NodeJs mongodb query to fetch collection data with total length offset and limit - Santosh Kumar Shah","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:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/","og_locale":"en_US","og_type":"article","og_title":"NodeJs mongodb query to fetch collection data with total length offset and limit - Santosh Kumar Shah","og_description":"This is how we query from mongodb collection to fetch data with total length, offset and limit. For total length we need to use async.","og_url":"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/","og_site_name":"Santosh Kumar Shah","article_published_time":"2020-12-16T08:15:30+00:00","author":"Santosh Kumar Shah","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Santosh Kumar Shah","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/","url":"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/","name":"NodeJs mongodb query to fetch collection data with total length offset and limit - Santosh Kumar Shah","isPartOf":{"@id":"https:\/\/santosh-shah.com\/blog\/#website"},"datePublished":"2020-12-16T08:15:30+00:00","author":{"@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f"},"breadcrumb":{"@id":"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/santosh-shah.com\/blog\/nodejs-mongodb-query-to-fetch-collection-data-with-total-length-offset-and-limit\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/santosh-shah.com\/blog\/"},{"@type":"ListItem","position":2,"name":"NodeJs mongodb query to fetch collection data with total length offset and limit"}]},{"@type":"WebSite","@id":"https:\/\/santosh-shah.com\/blog\/#website","url":"https:\/\/santosh-shah.com\/blog\/","name":"Santosh Kumar Shah","description":"JavaScript Developer","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/santosh-shah.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f","name":"Santosh Kumar Shah","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g","caption":"Santosh Kumar Shah"},"description":"I am JavaScript developer.","sameAs":["https:\/\/santosh-shah.com\/blog"],"url":"https:\/\/santosh-shah.com\/blog\/author\/sks7yu\/"}]}},"_links":{"self":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":0,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"wp:attachment":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}