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.
- aggregate(pipeline, options, callback)
- bulkWrite(operations, options, callback)
- count(query, options, callback)
- createIndex(fieldOrSpec, options, callback)
- createIndexes(indexSpecs, callback)
- deleteMany(filter, options, callback)
- deleteOne(filter, options, callback)
- distinct(key, query, options, callback)
- drop(callback)
- dropIndex(indexName, options, callback)
- dropIndexes(callback)
- find(query, projection)
- findAndModify(query, sort, doc, options, callback)
- findOne(query, projection, callback)
- findOneAndDelete(filter, options, callback)
- findOneAndReplace(filter, replacement, options, callback)
- findOneAndUpdate(filter, update, options, callback)
- findOneAndUpsert(filter, update, options, callback)
- geoHaystackSearch(x, y, options, callback)
- geoNear(x, y, options, callback)
- group(keys, condition, initial, reduce, finalize, command, options, callback)
- indexes(callback)
- indexExists(indexes, callback)
- indexInformation(options, callback)
- initializeOrderedBulkOp(options, callback)
- initializeUnorderedBulkOp(options)
- insertMany(docs, options, callback)
- insertOne(doc, options, callback)
- isCapped(callback)
- listIndexes(options)
- mapReduce(map, reduce, options, callback)
- options(callback)
- parallelCollectionScan(options, callback)
- reIndex(callback)
- rename(newName, options, callback)
- replaceOne(filter, doc, options, callback)
- stats(options, callback)
- updateMany(filter, update, options, callback)
- updateOne(filter, update, options, callback)
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.
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
-
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.
-
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.
-
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
-
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
- obj, document to insert
- options, optional settings
- obj, inserted document
- options, optional settings
- objs, documents to insert
- options, optional settings
- objs, inserted documents
- options, optional settings
- updateOne
- findOneAndUpdate
- condition, query to select documents
- modifier, update operations
- options, optional settings
- updateOne
- findOneAndUpdate
- condition, query to select documents
- modifier, update operations
- options, optional settings
- result, operation result
- condition, query to select documents
- modifier, update operations
- options, optional settings
- condition, query to select documents
- modifier, update operations
- options, optional settings
- result, operation result
- deleteOne
- findOneAndDelete
- condition, query to select documents
- options, optional settings
- deleteOne
- findOneAndDelete
- condition, query to select documents
- options, optional settings
- result, operation result
- condition, query to select documents
- options, optional settings
- condition, query to select documents
- options, optional settings
- result, operation result
- replaceOne
- findOneAndReplace
- condition, query to select documents
- replacement, document to replace original
- options, optional settings
- replaceOne
- findOneAndReplace
- condition, query to select documents
- replacement, document to replace original
- options, optional settings
- result, operation result
- condition, query to select documents
- modifier, update operations
- options, optional settings
- condition, query to select documents
- modifier, update operations
- options, optional settings
- obj, upserted document
- isUpdated, flag indicating whether document was updated or inserted
-
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 options as second
-
options, object, null. Options that will be passed to plugin.
seqCollectionName
- name of sequences collection ('__sequences' by default)seqName
- name of sequence (collection name by default)key
- key in document to set sequence value (_id
by default)format
- date format that will be used ascreateDate
, available values:'timestamp'
(default) - integer timestamp date in milliseconds (.getTime()
)'string'
- string date (.toString()
)'ISOString'
- string date in ISO format (.toISOString()
)'ISODate'
- date as Date object (new Date()
)function(date)
- custom user function that should return formatted date
format
- date format that will be used asupdateDate
, available values:'timestamp'
(default) - integer timestamp date in milliseconds (.getTime()
)'string'
- string date (.toString()
)'ISOString'
- string date in ISO format (.toISOString()
)'ISODate'
- date as Date object (new Date()
)function(date)
- custom user function that should return formatted date
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.
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.
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.
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
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 | |
afterInsertOne | insertOne | |
beforeInsertMany | insertMany | |
afterInsertMany | insertMany | |
beforeUpdateOne | ||
afterUpdateOne | ||
beforeUpdateMany | updateMany | |
afterUpdateMany | updateMany | |
beforeDeleteOne | ||
afterDeleteOne | ||
beforeDeleteMany | deleteMany | |
afterDeleteMany | deleteMany | |
beforeReplaceOne | ||
afterReplaceOne | ||
beforeUpsertOne | findOneAndUpsert | |
afterUpsertOne | findOneAndUpsert |
Collection class allows to create and use plugins for adding auto-generated fields to documents, for example.
Add plugin to collection.
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.
options:
Add createDate to each inserted to collection document
options:
Add updateDate to each updated or replaces document
options:
Add field operation
with query info to error object