Mongodbext
Extension for node-mongodb-native that allows to add hooks on write operations, such as create, update, remove
Install / Use
/learn @2do2go/MongodbextREADME
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.
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
countmethod in favor ofcountDocumentsandestimatedDocumentCount. SettingcustomCountImplementationtotrueallows you to use under the hood ofcounteithercountDocumentsif query predicate exists orestimatedDocumentCountif 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.
- aggregate(pipeline, options, callback)
- bulkWrite(operations, options, callback)
- count(query, options, callback)
- countDocuments(query, options, callback)
- createIndex(fieldOrSpec, options, callback)
- createIndexes(indexSpecs, options, callback)
- deleteMany(filter, options, callback)
- deleteOne(filter, options, callback)
- distinct(key, query, options, callback)
- drop(options, callback)
- dropIndex(indexName, options, callback)
- dropIndexes(options, callback)
- estimatedDocumentCount(options, callback)
- find(query, projection)
- findOne(query, projection, callback)
- findOneAndDelete(filter, options, callback)
- findOneAndReplace(filter, replacement, options, callback)
- findOneAndUpdate(filter, update, options, callback)
- findOneAndUpsert(filter, update, options, callback)
- indexes(options, callback)
- indexExists(indexes, options, callback)
- indexInformation(options, callback)
- initializeOrderedBulkOp(options)
- initializeUnorderedBulkOp(options)
- insertMany(docs, options, callback)
- insertOne(doc, options, callback)
- isCapped(options, callback)
- listIndexes(options)
- mapReduce(map, reduce, options, callback)
- options(options, callback)
- parallelCollectionScan(options, callback)
- rename(newName, options, callback)
- replaceOne(filter, doc, options, callback)
- stats(options, callback)
- updateMany(filter, update, options, callback)
- updateOne(filter, update, options, callback)
- watch(pipeline, options)
<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:
Examples:
var
Related Skills
node-connect
349.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.7kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
