SkillAgentSearch skills...

Mongodbext

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

Install / Use

/learn @2do2go/Mongodbext
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

mongodbext

Build Status

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.

Important since version 3.0.0 mongodb drivers of versions 2.x.x are no longer supported.

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<string>, null. Set supported data changing methods. If not set all methods are supported.
    • customCountImplementation, boolean, null. Starting from 4.0 MongoDB deprecates count method in favor of countDocuments and estimatedDocumentCount. Setting customCountImplementation to true allows you to use under the hood of count either countDocuments if query predicate exists or estimatedDocumentCount if no query predicate provided.
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

Methods marked as deprecated are not present in documentation.

<a name="deletemany"></a>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'
			);
		});
	});
});

<a name="deleteone"></a>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'
			);
		});
	});
});

<a name="find"></a>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

Related Skills

View on GitHub
GitHub Stars8
CategoryDevelopment
Updated4y ago
Forks6

Languages

JavaScript

Security Score

70/100

Audited on Dec 8, 2021

No findings