title: How to count unique views to a blog post (logged in and non logged in user) using Node.Js, MongoDB, Mongoose, Express
body: <p>I want to display how many times a blog post has been read kind of like what Business Insider has.</p>
  
  <p>The objective is...</p>
  
  <ul>
  <li>View count that doesn't increment with every reload.</li>
  <li>Every blog post stores its own view count</li>
  <li><p>Fetch the view count from MongoDB/Mongoose Schema field and display it in HTML.</p>
  
  <pre><code>var express         = require('express');
  var cookieParser    = require('cookie-parser');
  var mongoose        = require('mongoose');
  var express-session = require('express-session');
  
  //Show Blog Post 
  
  router.get('/blog/:categorySlug/:slug', function (req, res)
    var slug = req.params.slug;
      Blog.findOne({'slug' : slug}).populate('comments').exec(function (err, foundBlog) {
        if (err) {
      console.log(err);
        } else {
       res.render('show', { main: foundBlog, title: foundBlog.title});
      }
    });
  });
  </code></pre></li>
  </ul>
  
  <p>I know that if i use <code>req.session.views</code>, it will increment the view count with every reload across all pages.</p>
  
  <pre><code>router.get('/blog/:categorySlug/:slug', function (req, res) { 
    req.session.views = (req.session.views || 0) + 1  
    var slug = req.params.slug;
      Blog.findOne({'slug' : slug}).populate('comments').exec(function (err, foundBlog) {
        if (err) {
          console.log(err);
        } else {
          res.render('show', { main: foundBlog, title: foundBlog.title, pageViewCount: req.session.views});
        }
      });
    });
  </code></pre>
  
  <p>So what could i do to save the view count of each blog separately and store that view count value in that blog post's <code>viewCount</code> field in the database and then render it in HTML using <code>&lt;%= main.viewCount %&gt;</code></p>
  
  <pre><code>    // Blog Schema
  
        var mainBlogSchema = new mongoose.Schema({
        image: String,
        imageDescription: String,
        priority: {
           type: String,
           default: ""
        },
        title: String,
        content: String,
        author: {
           id: {
           type: mongoose.Schema.Types.ObjectId,
           ref: 'User',
         },
           username: String,
           name: String,
        },
        slug: {
           type: String,
           unique: true,
        },
        status: String,
        viewCount: {
           type: Number,
           default: 0,
        },
        category: String,
        categorySlug: String,
        tags: String,
        updated: {
           type: Boolean,
           default: false
        },
        date: { type: Date, default: Date.now },
        comments: [
          {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Comment',
          },
         ],
       },{
       timestamps: {
         createdAt: 'createdAt',
         updatedAt: 'updatedAt'
        }
       });
  </code></pre>