Git Product home page Git Product logo

mongodbext's Introduction

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.

Installation

npm install mongodbext

Usage

new Collection(db, collectionName, options)

Creates new instance of collection

Parameters:

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.
Returns:

Instance of collection

Examples:
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');
	});
});

Collection methods

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

deleteMany(filter, options, callback)

Delete multiple documents on MongoDB

Parameters:

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

Returns:

Promise if no callback passed

Examples:
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'
			);
		});
	});
});

deleteOne(filter, options, callback)

Delete a document on MongoDB

Parameters:

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

Returns:

Promise if no callback passed

Examples:
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'
			);
		});
	});
});

find(query, projection)

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

Parameters:

All parameters described as name, type, default value.

  • filter, object. The Cursor query object.

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

Returns:

Cursor

Examples:
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');
			});
		});
	});
});

findOne(query, projection, callback)

Fetches the first document that matches the query

Parameters:

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

Returns:

Promise if no callback passed

Examples:
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});
		});
	});
});

findOneAndDelete(filter, options, callback)

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

Parameters:

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.

Returns:

Promise if no callback passed

Examples:
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'
			);
		});
	});
});

findOneAndReplace(filter, replacement, options, callback)

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

Parameters:

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.

Returns:

Promise if no callback passed

Examples:
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'
			);
		});
	});
});

findOneAndUpdate(filter, update, options, callback)

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

Parameters:

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

Returns:

Promise if no callback passed

Examples:
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'
			);
		});
	});
});

findOneAndUpsert(filter, update, options, callback)

Upsert a single document on MongoDB.

Parameters:
  • 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

Returns:

Promise if no callback passed

Examples:
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'
			);
		});
	});
});

insertMany(docs, options, callback)

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.

Parameters:

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

  • Returns:

    Promise if no callback passed

    Examples:
    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);
    	});
    });

    insertOne(doc, options, callback)

    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.

    Parameters:

    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.

    Returns:

    Promise if no callback passed.

    Examples
    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]);
    	});
    });

    replaceOne(filter, doc, options, callback)

    Replace a document on MongoDB

    Parameters:

    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.

    Returns:

    Promise if no callback passed.

    Examples:
    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'
    			);
    		});
    	});
    });

    updateMany(filter, update, options, callback)

    Update multiple documents on MongoDB

    Parameters:

    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

    Returns:

    Promise if no callback passed.

    Examples:
    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'
    			);
    		});
    	});
    });

    updateOne(filter, update, options, callback)

    Update a single document on MongoDB

    Parameters:
    • 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

    Returns:

    Promise if no callback passed.

    Examples:
    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'
    			);
    		});
    	});
    });

    Hooks

    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

    Plugins

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

    addPlugin(plugin, pluginParams)

    Add plugin to collection.

    Parameters:
    • 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.

    Examples:
    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']
    	});
    });

    Built-in plugins

    sequenceId

    Replace mongo-style object _id with number.

    createDate

    Add createDate to each inserted to collection document

    updateDate

    Add updateDate to each updated or replaces document

mongodbext's People

Contributors

anton-makarov avatar artzhookov avatar fleg avatar okv avatar pavelvlasov avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.