title: How to autoIncrement id's and post to mongodb using node js
body: <p>I want to increment id's automatically in the mongoDB while posting the data. I am able to attach date for the req.body. How to attach ids with auto incrementation?</p>
  
  <p>This is my post call:</p>
  
  <pre><code>router.post('/addVisualization', function (req, res, next) {
      MongoClient.connect(url, function (err, db) {
          if (err) throw err;
          var dbo = db.db(dbName);
          req.body.dateOfEntry = new Date();
           function getNextSequence(id) {
              var ret = db.counters.findAndModify(
              {
                query: { _id: id },
                update: { $inc: { seq: 1 } },
                new: true
              }
              );
               return ret.seq;
            }
            dbo.collection("visualization").insertOne(req.body, function (err, resDB) {
              if (err) {
                  throw err;
                  res.status(401);
                  res.send({
                      "status": 401,
                      "message": "Some error happened"
                  });
              }
              else {
                  console.log("1 document inserted");
                  res.status(201)
                  res.send({
                      "body": req.body,
                      "status": 201,
                      "message": "visualization has been added"
                  });
              }
          });
          db.close();
      });
  });
  </code></pre>