title: how to display a single prouct from database in mongodb and node.js
body: <p>we have created a e-commerce project in that while clicking on categories in UI it will renders to category page in that page it has show one product of each subcategory </p>
  
  <p>i have written the code in which the entire products with the specific category name is displaying how i can remove the extra products as of now i required only one product of each sub category of the particular category</p>
  
  <pre><code> the routing code is
  
  
      router.get("/shop/:category_name", function(req, res){
   //querystring
  var url = require('url');
  var url_parts = url.parse(req.url, true);
  var query = url_parts.query;
  
  var viewModel = {
  breadCrumb: [],
  categories : [],
  featuredProducts :[],
  products : [],
   };
  
   viewModel.breadCrumb.push({
        name : 'Home',
        class : 'breadcrumb-ordinary'
      });
      viewModel.breadCrumb.push({
        name : req.params.category_name,
        class : 'breadcrumb-active'
      });
  
   async.parallel({
  
    categories: function(cb){
      Category.find({}, cb);
    },
  
    categories1:function(cb){
      Category.find({category_name:req.params.category_name}, cb);
    }
  
   }, function(err, results) {
  
       if (err) throw err;
      viewModel.categories1=results.categories1;
      viewModel.categories=results.categories;
      res.render('shop/prod_grid',viewModel);
  
    });
  
  });
  </code></pre>
  
  <p>the view UI  code is </p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html lang="en-US"&gt; &lt;!--&lt;![endif]--&gt;
  &lt;head&gt; 
  &lt;/head&gt;
  &lt;link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"&gt;
  &lt;link rel="stylesheet" href="../../../shop-plugins/css/jplist-pagination.css"&gt;
  &lt;link rel="stylesheet" href="../../../shop-plugins/css/breadcrumb.css"&gt;
      &lt;body&gt;              
  
   {{&gt; shop/header}}
  &lt;!-- BAR --&gt;
   &lt;div class="shipping-wrap"&gt;
      &lt;div class="container"&gt;
          &lt;div class="row"&gt;
              &lt;div class="span12"&gt;
                  &lt;ul&gt;
                    &lt;li&gt;
                        {{#each breadCrumb}}
                          {{#if @first}}
                          &lt;a href="/" class="{{class}}"&gt;{{name}}&amp;nbsp;
                          {{else}}
                          &lt;a class="{{class}}"&gt;{{name}}&amp;nbsp;
                          {{/if}}
  
                          {{#if @last}}
                            {{else}}
                              &lt;i class="fa  fa-angle-double-right"&gt;&lt;/i&gt;&lt;/a&gt;
                          {{/if}}
                    {{/each}}
  
                    &lt;/li&gt;
                &lt;/ul&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;!-- BAR --&gt;
  &lt;!-- PRODUCT-OFFER --&gt;
  &lt;div class="product_wrap"&gt;
  &lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;div id="span9" class="span9 product-grid"&gt;
              &lt;div id="products" class="box jplist jplist-grid-view"&gt;
                  &lt;div class="jplist-panel box jplist-panel-top"&gt;
                      &lt;div 
                          class="jplist-drop-down"
                          data-control-type="items-per-page-drop-down"
                          data-control-name="paging"
                          data-control-action="paging"&gt;
                          &lt;ul id="per-pages"&gt;
                              &lt;li&gt;&lt;span data-number="3"&gt;3 per page&lt;/span&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;span data-number="6" data-default="true"&gt;6 per page&lt;/span&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;span data-number="9"&gt;9 per page&lt;/span&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;span data-number="12"&gt;12 per page&lt;/span&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/div&gt;
                      &lt;div 
                          class="jplist-drop-down"
                          data-control-type="sort-drop-down"
                          data-control-name="sort"
                          data-control-action="sort"&gt;
                          &lt;ul id="sorting"&gt;
                              &lt;li&gt;&lt;span data-path="default"&gt;Sort By&lt;/span&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;span data-path=".brand" data-order="asc" data-type="text"&gt;Brand A-Z&lt;/span&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;span data-path=".brand" data-order="desc" data-type="text"&gt;Brand Z-A&lt;/span&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;span data-path=".cost" data-order="asc" data-type="number"&gt;Cost Low-High&lt;/span&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;span data-path=".cost" data-order="desc" data-type="number"&gt;Cost High-Low&lt;/span&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/div&gt;
                      &lt;div 
                          class="jplist-label"
                          data-type="Page {current} of {pages}"
                          data-control-type="pagination-info"
                          data-control-name="paging"
                          data-control-action="paging"&gt;
                      &lt;/div&gt;
                      &lt;div 
                          class="jplist-views" 
                          data-control-type="views" 
                          data-control-name="views" 
                          data-control-action="views"
                          data-default="jplist-grid-view"&gt;
                      &lt;/div&gt;  
                  &lt;/div&gt;
  
                              {{#each  subcategoryprod}}     
  
                              &lt;div&gt;
                                  &lt;ul&gt;
  
                                          &lt;li&gt;&lt;a href="/prod_grid?cat={{../category_name}}&amp;subcat={{subcategory}}"&gt;{{subcategory}}&lt;/a&gt;&lt;/li&gt;
  
                                  &lt;/ul&gt;
                              &lt;/div&gt;
  
  {{/each}}
  
  
   &lt;!--category wise products--&gt;
                                   &lt;div id="jp-panel" class="list box text_shadow"&gt;
  
                                          {{#each subcategoryprod}}
  
                                              &lt;div id="jp-panel-item" class="list-item box"&gt;
                                                  &lt;div class="span3 product"&gt;
  
                                                      &lt;div&gt;
                                                          &lt;figure&gt;
                                                              {{#each image}}
                                                                  {{#if @first}}
                                                                      &lt;a href="#"&gt;&lt;img src="../../../uploads/products/{{../_id}}/{{img}}" alt="" onerror="this.src='../../../shop-images/coming-soon.png'"&gt;&lt;/a&gt;
                                                                      &lt;div class="overlay"&gt;
                                                                          &lt;a href="../../../uploads/products/{{../_id}}/{{img}}" class="zoom" width="1200" height="1200"&gt;&lt;/a&gt;
                                                                          &lt;a href="/prod_grid/{{../product_name}}/{{../product_no}}?cat={{../category}}&amp;subcat={{../subcategory}}&amp;tags={{../tags}}&amp;brand={{../brand}}"  class="link"&gt;&lt;/a&gt;
  
                                                                      &lt;/div&gt;
                                                                  {{/if}}
                                                                  {{else}}
                                                                      &lt;a href="#"&gt;&lt;img src="../../../shop-images/coming-soon.png" alt=""&gt;&lt;/a&gt;
                                                                      &lt;div class="overlay"&gt;
                                                                          &lt;a href="../../../shop-images/coming-soon.png" class="zoom" height="1200" width="1200"&gt;&lt;/a&gt;
                                                                          &lt;a href="/prod_grid/{{product_name}}/{{product_no}}?cat={{category}}&amp;subcat={{subcategory}}&amp;tags={{tags}}&amp;brand={{brand}}"  class="link"&gt;&lt;/a&gt;
  
                                                                      &lt;/div&gt;
                                                              {{/each}}
                                                          &lt;/figure&gt;
                                                          &lt;div class="detail"&gt;
                                                              &lt;span name="retailprice" class="cost"&gt;₹{{RetailPrice}}&lt;/span&gt;
                                                              &lt;span&gt;{{colour}}&lt;/span&gt;
                                                              &lt;h4 class="brand"&gt;{{brand}}&lt;/h4&gt;
                                                              &lt;span&gt;{{Description}}&lt;/span&gt;
                                                              &lt;div class="icon"&gt;
                                                                  &lt;a href="/shop/shopping-cart/{{product_no}}" class="one tooltip" title="Add to cart"&gt;&lt;/a&gt;
                                                                  &lt;a href="/product-grid" class="two tooltip " title="Add to wishlist"&gt;&lt;/a&gt;
                                                              &lt;/div&gt;
                                                          &lt;/div&gt;
                                                      &lt;/div&gt;
  
                                                  &lt;/div&gt;
                                              &lt;/div&gt;
                                      {{/each}}
  
                                          &lt;/div&gt;
  
  
  
  
  
  
  
  
                  &lt;div class="box jplist-no-results text-shadow align-center"&gt;
                      &lt;p&gt; No results Found&lt;/p&gt;
                  &lt;/div&gt;
                  &lt;div class="jplist-panel box panel-bottom"&gt;
                      &lt;div 
                          class="jplist-label"
                          data-type="{start} - {end} of {all}"
                          data-control-type="pagination-info"
                          data-control-name="paging"
                          data-control-action="paging"&gt;
                      &lt;/div&gt;
                      &lt;div 
                          class="jplist-pagination back-top"
                          data-control-animate-to-top="true"
                          data-control-type="pagination"
                          data-control-name="paging"
                          data-control-action="paging"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="span3"&gt;
              &lt;div class="row"&gt;&amp;nbsp;
              &lt;/div&gt;
              &lt;div id="sidebar"&gt;
                  &lt;div class="widget"&gt;
                      &lt;h4&gt;Price Filter&lt;/h4&gt;
                      &lt;div class="price-range"&gt;
                          &lt;div id="slider-range"&gt;&lt;/div&gt;
                          &lt;p class="clearfix"&gt;
                          &lt;input type="text" id="amount"  readonly /&gt;
                          &lt;input type="text" id="amount2" readonly /&gt;
                          &lt;/p&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="widget"&gt;
                      &lt;h4&gt;BRANDS&lt;/h4&gt;
                      &lt;div id=""&gt;
                          &lt;div class="brands"&gt;
                               &lt;input type='text' id='txtList' onkeyup="searchBrand(this)" placeholder="search brands here....." /&gt;
                                  &lt;!-- &lt;ul id="fromList" class="myid"&gt; --&gt;
                                      {{#each products}}
                                          &lt;ul id="fromList" class="myid"&gt;
                                              &lt;li&gt;&amp;nbsp;&amp;nbsp;&lt;input type="checkbox" id="{{brand}}"  class="brandFilterCheckBox pull-right" align="center"/&gt;&amp;nbsp;&amp;nbsp;&lt;label for="{{brand}}"&gt;{{brand}}&lt;label&gt;&lt;/li&gt;
                                          &lt;/ul&gt;
                                      {{/each}}
                                  &lt;!-- &lt;/ul&gt; --&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
                  &lt;div class="widget"&gt;
                      &lt;h4&gt;CATEGORIES&lt;/h4&gt;
                      &lt;div id="accordion"&gt;
                          {{#each categories}}
                              &lt;h5&gt;&lt;a href="#"&gt;{{category_name }}&lt;/a&gt;&lt;/h5&gt;
                              &lt;div&gt;
                                  &lt;ul&gt;
                                      {{#each subcategories1}}
                                          &lt;li&gt;&lt;a href="/prod_grid?cat={{../category_name}}&amp;subcat={{subcategory_name}}"&gt;{{subcategory_name}}&lt;/a&gt;&lt;/li&gt;
                                      {{/each}}
                                  &lt;/ul&gt;
                              &lt;/div&gt;
                          {{/each}}
                      &lt;/div&gt;
                  &lt;/div&gt;
  
          &lt;/div&gt;
      &lt;/div&gt;
   &lt;/div&gt; 
   &lt;/div&gt;
    &lt;!-- Featured Products --&gt;
          &lt;div class="container" style="margin-top:10px"&gt;
                      &lt;div class="row heading-wrap"&gt;
                          &lt;div class="span12 heading"&gt;
                              &lt;h2&gt;Featured Products &lt;span&gt;&lt;/span&gt;&lt;/h2&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="row similar_products"&gt;
                          &lt;div id="feat_prod"&gt;                                
                          {{#each featuredProducts}}
                              &lt;div class="span3 product"&gt;
                                      &lt;figure&gt;
                                          {{#each image}}
                                              {{#if @first}}
                                                  &lt;a href="#"&gt;&lt;img src="../../../uploads/products/{{../_id}}/{{img}}" alt=""&gt;&lt;/a&gt;
                                                  &lt;div class="overlay"&gt;
                                                      &lt;a href="../../../uploads/products/{{../_id}}/{{img}}" class="zoom prettyPhoto"&gt;&lt;/a&gt;
                                                      &lt;a href="/prod_grid/{{../product_name}}/{{../product_no}}?cat={{../category}}&amp;subcat={{../subcategory}}&amp;tags={{../tags}}&amp;brand={{../brand}}"  class="link"&gt;&lt;/a&gt;
                                                  &lt;/div&gt;
                                                  {{/if}}
                                          {{/each}}
  
                                      &lt;/figure&gt;
                                      &lt;div class="detail"&gt;
                                          &lt;span&gt;₹{{RetailPrice}}&lt;/span&gt;
                                          &lt;h4&gt;{{product_name}}&lt;/h4&gt;
                                          &lt;span&gt;{{Description}}&lt;/span&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                          {{/each}}
                          &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  
    &lt;!-- PRODUCT-OFFER --&gt;
       &lt;!-- FOOTER --&gt;
    {{&gt; shop/footer}}
               &lt;script type="text/javascript" src="../../../shop-plugins/js/jplist-pagination.js"&gt;&lt;/script&gt;
               &lt;script src="../../../bootstrap/js/star.min.js"&gt;&lt;/script&gt;
   &lt;script src="../../../shop-plugins/js/jquery.paginate.js"&gt;    &lt;/script&gt;                
       &lt;script&gt;
          $(function(){
              $('#feat_prod').paginate();
          });
       &lt;/script&gt;       
  
  
     &lt;script&gt;
  
      $(document).ready(function(){
              $('#products').jplist({
             itemsBox: '.list' 
              ,itemPath: '.list-item' 
              ,panelPath: '.jplist-panel'
  
          });           
      }); 
  
   &lt;/script&gt;
  
  
    &lt;script&gt;
    $('document').ready(function(){
          $("#slider-range").slider({change : function(event,ui){
           var lowerLimit=$("#amount").val();
              var numberLowerLimit=Number(lowerLimit.substring(1,lowerLimit.length));
             var upperLimit=$("#amount2").val();
             upperLimit=upperLimit.substring(1,upperLimit.length);
              var numberUpperLimit=Number(upperLimit.replace("₹",""));
             console.log("LOWER LIMIT :"+lowerLimit+" UPPER LIMIT :"+upperLimit);
             $(".cost").each(function(){
                  var rowUnitCost=$(this).text();
                  var unitCost=Number(rowUnitCost.substring(1,rowUnitCost.length));
                  if(unitCost&lt;numberLowerLimit || unitCost&gt;numberUpperLimit){
                      var hiddingBox=$(this).parents(".list-item");
                       /*hiddingBox.removeClass("list-item").addClass("rafsal-test");*/
                      hiddingBox.hide();
                      console.log("Values IN:"+unitCost);
                  }
                  else{
                      var hiddingBox=$(this).parents(".list-item");
                      if(hiddingBox.attr('id')==undefined || hiddingBox.attr('id')=="undefined"){
                          hiddingBox=$(this).parents(".rafsal-test");
                          hiddingBox.addClass("list-item").removeClass("rafsal-test");
                      }
                      hiddingBox.show();
                      console.log("Values OUT:"+unitCost);
                  }
                   $('#products').jplist({
                      itemsBox: '.list' ,
                      itemPath: '.list-item' ,
                      panelPath: '.jplist-panel'
  
                  });
  
              });
          }
      });
  
      // Brand Filter
      $(".brandFilterCheckBox").on('click',function(){
          $(".brand").each(function(){
              var hiddingBox=$(this).parents(".list-item");
              hiddingBox.removeClass("list-item").addClass("rafsal-test");
              hiddingBox.hide();
          });
          $(".brandFilterCheckBox").each(function(){
              if($(this).prop("checked")){
                  var filterBrand=$(this).attr("id");   
                  $(".brand").each(function(){
                      var unitBrandName=$(this).text().trim();
  
                      if(unitBrandName== filterBrand){
                          console.log(unitBrandName);
                          var hiddingBox=$(this).parents(".list-item");
                          if(hiddingBox.attr('id')==undefined || hiddingBox.attr('id')=="undefined"){
                              hiddingBox=$(this).parents(".rafsal-test");
                              hiddingBox.addClass("list-item").removeClass("rafsal-test");
                          }
                          hiddingBox.show();
                      }
  
                  });
                  $('#products').jplist({
                      itemsBox: '.list' ,
                      itemPath: '.list-item' ,
                      panelPath: '.jplist-panel'
  
                  });
              }
          });
  
      })
      var textCheck="##";
      removeDuplicateBrands()
    });
   function removeDuplicateBrands(){
      var textCheck="##";
     $(".brandFilterCheckBox").each(function(){
          $(this).attr('id');
          if(textCheck.indexOf("##"+$(this).attr('id')+"##")==(-1)){
              textCheck=textCheck+$(this).attr('id')+"##";
  
          }
          else{
              $(this).parent("li").hide();
  
          }
      }); 
  }
  &lt;/script&gt;
  &lt;script&gt;
  function searchBrand(element) {
   var value = $(element).val();
   $("#fromList li").each(function () {
      if ($(this).text().search(value) &gt; -1) {
          $(this).show();
          $(this).prevAll('.header').first().show();
      } else {
          $(this).hide();
      }
  });
  removeDuplicateBrands();
  }
  
   &lt;/script&gt;
  
      &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>so i am posting a photo in which my output is there it contains all products of a specified category i need only one product from each sub category of the specified category and the image contains four products of protection category and two sub categories air freshners and Head unit , air freshners contains three products i need to display only one from airfreshner and one from head unit in which we need<a href="https://i.stack.imgur.com/G52wc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/G52wc.png" alt="enter image description here"></a></p>