Registry / database / mongo-oplog

mongo-oplog

JSON →
library2.1.4jsnpmunverified

Watch MongoDB oplog (replica set operation log) for real-time changes in Node.js. Version 2.1.4 supports Promise/async-await, event-based streaming using eventemitter3, and filtering by namespace (database/collection). Requires MongoDB replica set configuration. Built on the native MongoDB driver. Breaking changes in 2.x dropped old callback API in favor of promises. Differentiators: lightweight, small footprint, backward-compatible callbacks in v2, and strong readability with XO.

npm install mongo-oplog
INSTALL
IMPORT
SIG · MONGO-OPLOG
M
mongo-oplog
databasejavascriptv2.1.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import MongoOplog from 'mongo-oplog'
const { default: MongoOplog } = require('mongo-oplog')
Default ES module export since v2. Do not destructure a named export.
MongoOplog
const MongoOplog = require('mongo-oplog')
const { MongoOplog } = require('mongo-oplog')
CommonJS yields a function (not an object with MongoOplog property) since v2.
types
import type { OplogEvent } from 'mongo-oplog'
import { OplogEvent } from 'mongo-oplog'
Package ships TypeScript declarations since v2.1.0. Use 'import type' for type-only imports.

Initialize mongo-oplog, start tailing the oplog, and listen for insert/update/delete events. Stop after 10 seconds.

import MongoOplog from 'mongo-oplog'; const uri = process.env.MONGO_URI || 'mongodb://127.0.0.1:27017/local'; const options = { ns: 'test.posts' }; const oplog = MongoOplog(uri, options); oplog.tail() .then(() => console.log('Tailing started')) .catch(err => console.error('Failed to tail:', err)); oplog.on('insert', (doc) => { console.log('Inserted:', doc); }); oplog.on('update', (doc) => { console.log('Updated:', doc); }); oplog.on('delete', (doc) => { console.log('Deleted:', doc.o._id); }); oplog.on('error', (err) => { console.error('Oplog error:', err); }); // Stop after 10 seconds setTimeout(() => { oplog.stop().then(() => console.log('Stopped')); }, 10000);
Debug
Known issues
breakingVersion 2.x removed callback API; use promises or async/await
fix
Replace callbacks with .then()/await; see UPGRADE.md
affects: >=2.0.0 <2.0.0
gotchaMongoDB must be configured as a replica set; standalone mode has no oplog
fix
Start mongod with --replSet rs0 and run rs.initiate() in mongo shell
affects: >=0.0.0
deprecatedNode.js v6 and v7 support is deprecated; use Node >= 8
fix
Upgrade Node.js to 8+ or use v1.x
affects: >=2.0.0 <2.1.0
gotchaNamespace option ns must match exactly 'database.collection' pattern
fix
Use 'dbName.collectionName' (case-sensitive) or regex string like '/test.posts/'
affects: >=0.0.0
deprecatedDirect connection to a mongod without replica set name may cause silent failures
fix
Ensure connection string includes replicaSet=rs0 or use an existing replica set member
affects: >=2.0.0
Errors
Common errors & fixes
MongoError: not master and slaveOk=false
Connection to a secondary replica set member without slaveOk flag
fix
Add ?slaveOk=true to the URI or set options.slaveOk: true
AssertionError: The uri must include a valid replica set name
MongoDB driver requires replica set name when connecting to multiple hosts
fix
Add ?replicaSet=rs0 to the URI
TypeError: oplog.tail is not a function
Importing wrong export (named instead of default)
fix
Use import MongoOplog from 'mongo-oplog' (default import)
MongoError: no such cmd: 'getLog'
Connecting to MongoDB standalone instance without oplog
fix
Start MongoDB with --replSet and run rs.initiate()
Upgrade
Version history
2.1.4latest on npm
Audit
Dependencies
mongodbrequiredNative MongoDB driver for connecting and querying oplog
debugrequiredFor debugging logs
eventemitter3requiredHigh-performance event emitter for streaming events
Agent activity
4 hits · last 30 days
node
4
Resources
mongo-oplog — npm install mongo-oplog · libregistry