mongo-created-modified

Mongo collection decorator that add created and modified timestamp on documents

Usage no npm install needed!

<script type="module">
  import mongoCreatedModified from 'https://cdn.skypack.dev/mongo-created-modified';
</script>

README

mongo-created-modified

Decorator for a mongo collection that add created and modified timestamp to the documents.

A created-modified document will have the properties createdDate sand lastModifiedDate.

{
  createdDate: new Date(),
  lastModifiedDate: new Date(),
  data: ...
}

Usage:

var mongoCreatedModified = require('mongo-created-modified');

mongodb.Db.connect(connection, function(err, db){
  if(err) throw err;

  database.collection('createdModifiedCollection', function (err, col) {
    if(err) throw err;

    var createdModifiedCollection = mongoCreatedModified(col);

    createdModifiedCollection.insert({a: 1}, function(err, doc) {
      if(err) throw err;

      createdModifiedCollection.findAnsModify({a: 1}, [], {a: 2},function (err, doc) {
        if(err) throw err;

        // returns document with createdDate and lastModifiedDate
        console.log(doc);
      });
    })
  });
});