Monitor your mongodb connection for bad queries in the client-side.
npm i mongodb-watcher
//connect to mongodb
const watcher = new MongoWatcher(db, {
longCursorThreshold: 100,
largeInsertThreshold: 1024 * 30,
largeFetchThreshold: 1024 * 30
});
watcher.on('long.cursor.enumeration', (data) => {
logger.error(`Detected bad query over ${data.collection} returning ${data.count} documents. \n ${data.stack}`);
});
db.collection('apples').find().toArray((apples) => {
res.json(apples);
});
This event is emitted when a cursor is enumerated (.toArray) returning an array of documents with more than longCursorThreshold
(defaults 100).
The data of the event contains:
collection
: the name of the collectioncount
: the number of documents returned or insertedstack
: an stack trace of to identify where the call was madecmd
: it contains thequery
,limit
,skip
,readPreference
,slaveOk
, etc.
This event is emitted when calling .insert([])
or .save
with a document of size greater than bigInsertThreshold
(defaults 30k).
collection
: the name of the collectiondocumentId
: thedocument._id
valuesize
: the size of the doc in bytescmd
: it contains thequery
,limit
,skip
,readPreference
,slaveOk
, etcstack
: an stack trace of to identify where the call was made
This event is emitted when retrieving a big document from the database.
collection
: the name of the collectiondocumentId
: thedocument._id
valuesize
: the size of the doc in bytesstack
: an stack trace of to identify where the call was made
More events are welcome.
MIT 2016 . JOSE F. ROMANIELLO