node package manager

mongodbext

Extension for node-mongodb-native that allows to add hooks on write operations, such as create, update, remove

mongodbext

This is extension for node-mongodb-native that imports patched collection object and allows to add hooks on write operations, such as insert, update and delete. It also adds some options to this operations, that allows to modify operation;s result.

npm install mongodbext

Creates new instance of collection

All parameters described as name, type, default value.

  • db, object. Database instance

  • collectionName, string. Name of collection.

  • options, object, null. Optional settings.

    • changeDataMethods, Array, null. Set supported data changing methods. If not set all methods are supported.

Instance of collection

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'constructorExample');
 
    collection.insertOne({a: 1}, function(err) {
        expect(err).not.ok();
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'constructorExample', {
        changeDataMethods: ['insertMany']
    });
 
    collection.insertOne({a: 1}, function(err) {
        expect(err).ok();
        expect(err.name).equal('MongoError');
        expect(err.message).equal('Method "insertOne" for collection "test" is not supported');
    });
});

All methods that marked as deprecated, such as insert, update etc., except for findAndModify, were removed from collection class and throw NotSupported error.

Delete multiple documents on MongoDB

All parameters described as name, type, default value.

  • filter, object. The Filter used to select the documents to remove

  • options, object, null. Optional settings.

    • w, number or string, null. The write concern.

    • wtimeout, number, null. The write concern timeout.

    • j, boolean, false. Specify a journal write concern.

    • returnResultOnly, boolean, true. Specifying result returning in callback.

  • callback, function. The command result callback

Promise if no callback passed

 
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'deleteManyExample');
 
    collection.insertMany([{
        a: 1
    }, {
        a: 2
    }], function(err, insertResult) {
        collection.deleteMany({}, function(err, deleteManyResult) {
            expect(deleteManyResult).only.keys('deletedCount');
        });
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'deleteManyExample');
 
    collection.insertMany([{
        a: 1
    }, {
        a: 2
    }], function(err, insertResult) {
        collection.deleteMany({}, {
            returnDocsOnly: false
        }, function(err, deleteManyResult) {
            expect(deleteManyResult).only.keys(
                'connection', 'result', 'deletedCount'
            );
        });
    });
});

Delete a document on MongoDB

All parameters described as name, type, default value.

  • filter, object. The Filter used to select the documents to remove

  • options, object, null. Optional settings.

    • w, number or string, null. The write concern.

    • wtimeout, number, null. The write concern timeout.

    • j, boolean, false. Specify a journal write concern.

    • bypassDocumentValidation, boolean, false. Allow driver to bypass schema validation in MongoDB 3.2 or higher.

    • returnResultOnly, boolean, true. Specifying result returning in callback.

  • callback, function. The command result callback

Promise if no callback passed

 
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'deleteOneExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.deleteOne({}, function(err, deleteOneResult) {
            expect(deleteOneResult).only.keys('deletedCount');
        });
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'deleteOneExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.deleteOne({}, {
            returnDocsOnly: false
        }, function(err, deleteOneResult) {
            expect(deleteOneResult).only.keys(
                'connection', 'result', 'deletedCount'
            );
        });
    });
});

Creates a cursor for a query that can be used to iterate over results from MongoDB

All parameters described as name, type, default value.

  • filter, object. The Cursor query object.

  • projection, object, null. The field projection object.

Cursor

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'findExample');
 
    collection.insertMany([{
        a: 1
    }, {
        a: 2
    }], function(err, insertResult) {
        collection.find({}, {
            _id: 0
        }).toArray(function(err, findResult) {
            findResult.forEach(function(obj) {
                expect(obj).not.key('_id');
            });
        });
    });
});

Fetches the first document that matches the query

All parameters described as name, type, default value.

  • filter, object. Query for find Operation.

  • projection, object, null. The field projection object.

  • callback, function. The command result callback

Promise if no callback passed

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'findOneExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.findOne({}, {
            _id: 0
        }, function(err, findOneResult) {
            expect(findOneResult).eql({a: 1});
        });
    });
});

Find a document and delete it in one atomic operation, requires a write lock for the duration of the operation.

All parameters described as name, type, default value.

  • filter, object. Document selection filter.

  • options, object, null. Optional settings.

    • projection, object, null. Limits the fields to return for all matching documents.

    • sort, object, null. Determines which document the operation modifies if the query selects multiple documents.

    • maxTimeMS, number, null. The maximum amount of time to allow the query to run.

    • returnDocsOnly, boolean, true. When true returns only result document.

  • callback, function. The collection result callback.

Promise if no callback passed

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'findOneAndDeleteExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.findOneAndDelete({
            _id: insertResult._id
        }, function(err, deleteResult) {
            expect(deleteResult).eql(insertResult);
        });
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'findOneAndDeleteExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.findOneAndDelete({
            _id: insertResult._id
        }, {
            returnDocsOnly: false
        }, function(err, deleteResult) {
            expect(deleteResult).keys(
                'value', 'ok', 'lastErrorObject'
            );
        });
    });
});

Find a document and replace it in one atomic operation, requires a write lock for the duration of the operation.

All parameters described as name, type, default value.

  • filter, object. Document selection filter.

  • replacement, object. Document replacing the matching document.

  • options, object, null. Optional settings.

    • projection, object, null. Limits the fields to return for all matching documents.

    • sort, object, null. Determines which document the operation modifies if the query selects multiple documents.

    • maxTimeMS, number, null. The maximum amount of time to allow the query to run.

    • returnOriginal, boolean, true. When false, returns the updated document rather than the original.

    • returnDocsOnly, boolean, true. When true returns only result document.

  • callback, function. The collection result callback.

Promise if no callback passed

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'findOneAndReplaceExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.findOneAndReplace({
            _id: insertResult._id
        }, {
            a: 2
        }, function(err, replaceResult) {
            expect(replaceResult).eql(insertResult);
        });
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'findOneAndReplaceExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.findOneAndReplace({
            _id: insertResult._id
        }, {
            a: 2
        }, {
            returnDocsOnly: false
        }, function(err, replaceResult) {
            expect(replaceResult).keys(
                'value', 'ok', 'lastErrorObject'
            );
        });
    });
});

Find a document and update it in one atomic operation, requires a write lock for the duration of the operation.

All parameters described as name, type, default value.

  • filter, object. Document selection filter.

  • update, object. Update operations to be performed on the document

  • options, object, null. Optional settings.

    • projection, object, null. Limits the fields to return for all matching documents.

    • sort, object, null. Determines which document the operation modifies if the query selects multiple documents.

    • maxTimeMS, number, null. The maximum amount of time to allow the query to run.

    • returnOriginal, boolean, true. When false, returns the updated document rather than the original.

    • returnDocsOnly, boolean, true. When true returns only result document.

  • callback, function. The collection result callback

Promise if no callback passed

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'findOneAndUpdateExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.findOneAndUpdate({
            _id: insertResult._id
        }, {
            $inc: {a: 1}
        }, function(err, updateResult) {
            expect(updateResult).eql(insertResult);
        });
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'findOneAndUpdateExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.findOneAndUpdate({
            _id: insertResult._id
        }, {
            $inc: {a: 1}
        }, {
            returnDocsOnly: false
        }, function(err, updateResult) {
            expect(updateResult).keys(
                'value', 'ok', 'lastErrorObject'
            );
        });
    });
});

Upsert a single document on MongoDB.

  • filter, object. The Filter used to select the document to upsert

  • update, object. The update operations or replacement document to be applied to the document

  • options, object, null. Optional settings.

    • projection, object, null. Limits the fields to return for all matching documents.

    • sort, object, null. Determines which document the operation modifies if the query selects multiple documents.

    • maxTimeMS, number, null. The maximum amount of time to allow the query to run.

    • returnOriginal, boolean, true. When false, returns the updated document rather than the original.

    • returnDocsOnly, boolean, true. When true returns only result document.

  • callback, function. The command result callback

Promise if no callback passed

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'findOneAndUpsertExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.findOneAndUpsert({
            _id: insertResult._id
        }, {
            $inc: {a: 1}
        }, function(err, upsertResult) {
            expect(upsertResult).eql(insertResult);
        });
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'findOneAndUpsertExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.findOneAndUpsert({
            _id: insertResult._id
        }, {
            $inc: {a: 1}
        }, {
            returnDocsOnly: false
        }, function(err, upsertResult) {
            expect(upsertResult).keys(
                'value', 'ok', 'lastErrorObject'
            );
        });
    });
});

Inserts an array of documents into MongoDB. If documents passed in do not contain the _id field, one will be added to each of the documents missing it by the driver, mutating the document. This behavior can be overridden by setting the forceServerObjectId flag.

All parameters described as name, type, default value.

  • docs, Array. Documents to insert.

  • options, object, null. Optional settings.

    • w, number | string, null. The write concern.

    • wtimeout, number, null. The write concern timeout.

    • j, boolean, false. Specify a journal write concern.

    • serializeFunctions, boolean, false. Serialize functions on any object.

    • forceServerObjectId, boolean, false. Force server to assign _id values instead of driver.

    • returnDocsOnly, boolean, true. When true returns only result documents.

  • callback, function. The command result callback

Promise if no callback passed

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'insertManyExample');
 
    var documents = [{_id: 1, a: 1}, {_id: 2, a: 2}];
    collection.insertMany(documents, function(err, insertResult) {
        expect(insertResult).eql(documents);
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'insertManyExample');
 
    var documents = [{_id: 1, a: 1}, {_id: 2, a: 2}];
    collection.insertMany(documents, {
        returnDocsOnly: false
    }, function(err, insertResult) {
        expect(insertResult).keys(
            'result', 'ops', 'insertedCount', 'insertedIds'
        );
        expect(insertResult.ops).eql(documents);
    });
});

Inserts a single document into MongoDB. If documents passed in do not contain the _id field, one will be added to each of the documents missing it by the driver, mutating the document. This behavior can be overridden by setting the forceServerObjectId flag.

All parameters described as name, type, default value.

  • doc, object. Document to insert.

  • options, object, null. Optional settings.

    • w, number | string, null. The write concern.

    • wtimeout, number, null. The write concern timeout.

    • j, boolean, false. Specify a journal write concern.

    • serializeFunctions, boolean, false. Serialize functions on any object.

    • forceServerObjectId, boolean, false. Force server to assign _id values instead of driver.

    • bypassDocumentValidation, boolean, false. Allow driver to bypass schema validation in MongoDB 3.2 or higher.

    • returnDocsOnly, boolean, true. When true returns only result document.

  • callback, function. The command result callback.

Promise if no callback passed.

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'insertOneExample');
 
    var document = {_id: 1, a: 1};
    collection.insertOne(document, function(err, insertResult) {
        expect(insertResult).eql(document);
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'insertOneExample');
 
    var documents = {_id: 1, a: 1};
    collection.insertOne(documents, {
        returnDocsOnly: false
    }, function(err, insertResult) {
        expect(insertResult).keys(
            'result', 'ops', 'insertedCount', 'insertedId', 'connection'
        );
        expect(insertResult.ops).eql([document]);
    });
});

Replace a document on MongoDB

All parameters described as name, type, default value.

  • filter, object. The Filter used to select the document to update

  • doc, object. The Document that replaces the matching document

  • options, object, null. Optional settings.

    • w, number | string, null. The write concern.

    • wtimeout, number, null. The write concern timeout.

    • j, boolean, false. Specify a journal write concern.

    • bypassDocumentValidation, boolean, false. Allow driver to bypass schema validation in MongoDB 3.2 or higher.

    • returnResultOnly, boolean, true. Specifying result returning in callback.

  • callback, function. The command result callback.

Promise if no callback passed.

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'replaceOneExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.replaceOne({
            _id: insertResult._id
        }, {
            a: 2
        }, function(err, replaceResult) {
            expect(replaceResult).keys(
                'matchedCount', 'modifiedCount', 'upsertedId'
            );
        });
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'replaceOneExample');
 
    collection.insertOne({
        a: 1
    }, function(err, insertResult) {
        collection.replaceOne({
            _id: insertResult._id
        }, {
            a: 2
        }, {
            returnDocsOnly: false
        }, function(err, replaceResult) {
            expect(replaceResult).keys(
                'result', 'connection', 'matchedCount', 'modifiedCount',
                'upsertedId', 'upsertedCount', 'ops'
            );
        });
    });
});

Update multiple documents on MongoDB

All parameters described as name, type, default value.

  • filter, object. The Filter used to select the document to update

  • update, object. The update operations to be applied to the document

  • options, object, null. Optional settings.

    • w, number | string, null. The write concern.

    • wtimeout, number, null. The write concern timeout.

    • j, boolean, false. Specify a journal write concern.

    • returnResultOnly, boolean, true. Specifying result returning in callback.

  • callback, function. The command result callback

Promise if no callback passed.

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'updateManyExample');
 
    var documents = [{_id: 1, a: 1}, {_id: 2, a: 1}];
    collection.insertMany(documents, function(err, insertResult) {
        collection.updateMany({
            a: 1
        }, {
            a: {$inc: 1}
        }, function(err, updateResult) {
            expect(updateResult).keys(
                'matchedCount', 'modifiedCount', 'upsertedId'
            );
        });
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'updateManyExample');
 
    var documents = [{_id: 1, a: 1}, {_id: 2, a: 1}];
    collection.insertMany(documents, function(err, insertResult) {
        collection.updateMany({
            a: 1
        }, {
            a: {$inc: 1}
        }, {
            returnResultOnly: false
        }, function(err, updateResult) {
            expect(updateResult).keys(
                'connection', 'result', 'matchedCount', 'modifiedCount',
                'upsertedId', 'upsertedCount'
            );
        });
    });
});

Update a single document on MongoDB

  • filter, object. The Filter used to select the document to update

  • update, object. The update operations to be applied to the document

  • options, object, null. Optional settings.

    • w, number | string, null. The write concern.

    • wtimeout, number, null, The write concern timeout.

    • j, boolean, false. Specify a journal write concern.

    • bypassDocumentValidation, boolean, false. Allow driver to bypass schema validation in MongoDB 3.2 or higher.

    • returnResultOnly, boolean, true. Specifying result returning in callback.

  • callback, function. The command result callback

Promise if no callback passed.

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'updateOneExample');
 
    var document = {_id: 1, a: 1};
    collection.insertOne(document, function(err, insertResult) {
        collection.updateOne({
            a: 1
        }, {
            a: {$inc: 1}
        }, function(err, updateResult) {
            expect(updateResult).keys(
                'matchedCount', 'modifiedCount', 'upsertedId'
            );
        });
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection,
    expect = require('expect.js');
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'updateOneExample');
 
    var document = {_id: 1, a: 1};
    collection.insertOne(document, function(err, insertResult) {
        collection.updateOne({
            a: 1
        }, {
            a: {$inc: 1}
        }, {
            returnResultOnly: false
        }, function(err, updateResult) {
            expect(updateResult).keys(
                'connection', 'result', 'matchedCount', 'modifiedCount',
                'upsertedId', 'upsertedCount'
            );
        });
    });
});

All hooks take two parameters: params and callback. Callback is always callback function, and params fields depends on hook.

Name Methods Params fields
beforeInsertOne insertOne
  • obj, document to insert
  • options, optional settings
afterInsertOne insertOne
  • obj, inserted document
  • options, optional settings
beforeInsertMany insertMany
  • objs, documents to insert
  • options, optional settings
afterInsertMany insertMany
  • objs, inserted documents
  • options, optional settings
beforeUpdateOne
  • updateOne
  • findOneAndUpdate
  • condition, query to select documents
  • modifier, update operations
  • options, optional settings
afterUpdateOne
  • updateOne
  • findOneAndUpdate
  • condition, query to select documents
  • modifier, update operations
  • options, optional settings
  • result, operation result
beforeUpdateMany updateMany
  • condition, query to select documents
  • modifier, update operations
  • options, optional settings
afterUpdateMany updateMany
  • condition, query to select documents
  • modifier, update operations
  • options, optional settings
  • result, operation result
beforeDeleteOne
  • deleteOne
  • findOneAndDelete
  • condition, query to select documents
  • options, optional settings
afterDeleteOne
  • deleteOne
  • findOneAndDelete
  • condition, query to select documents
  • options, optional settings
  • result, operation result
beforeDeleteMany deleteMany
  • condition, query to select documents
  • options, optional settings
afterDeleteMany deleteMany
  • condition, query to select documents
  • options, optional settings
  • result, operation result
beforeReplaceOne
  • replaceOne
  • findOneAndReplace
  • condition, query to select documents
  • replacement, document to replace original
  • options, optional settings
afterReplaceOne
  • replaceOne
  • findOneAndReplace
  • condition, query to select documents
  • replacement, document to replace original
  • options, optional settings
  • result, operation result
beforeUpsertOne findOneAndUpsert
  • condition, query to select documents
  • modifier, update operations
  • options, optional settings
afterUpsertOne findOneAndUpsert
  • condition, query to select documents
  • modifier, update operations
  • options, optional settings
  • obj, upserted document
  • isUpdated, flag indicating whether document was updated or inserted

Collection class allows to create and use plugins for adding auto-generated fields to documents, for example.

Add plugin to collection.

  • plugin, function | string. It can be either plugin function, or in case of built-in plugins it's name. Plugin function takes collection object as first parameter and optional pluginParams as second

  • pluginParams, object, null. Params that will be passed to plugin.

var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection;
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'updateOneExample');
 
    collection.addPlugin(function(collection) {
        collection.on('insertOne', function(params, callback) {
            params.obj.counter = 1;
            callback();
        });
    });
});
var MongoClient = require('mongodb').MongoClient,
    Collection = require('mongodbext').Collection;
 
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var collection = new Collection(db, 'updateOneExample');
 
    collection.addPlugin('sequenceId');
    collection.addPlugin('unsupportedMethods', {
        methods: ['insertMany', 'updateMany']
    });
});

Replace mongo-style object _id with number.

Add createDate to each inserted to collection document

Add updateDate to each updated or replaces document