Registry / storage / pino-mongodb

pino-mongodb

JSON →
library5.0.0jsnpmunverified

Pino transport for inserting structured JSON logs into MongoDB. Current stable version is 5.0.0, released in 2024. This package is part of the Pino logger ecosystem and supports both modern Pino transports and legacy CLI usage. It allows users to pipe log streams directly into MongoDB collections, with options for custom parsing and authentication. Compared to other MongoDB log transports, it integrates seamlessly with Pino's pipeline and can handle high-throughput logging. The library is actively maintained with frequent releases.

npm install pino-mongodb
INSTALL
IMPORT
SIG · PINO-MONGODB
P
pino-mongodb
storagejavascriptv5.0.0
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.

pino
import pino from 'pino'
const pino = require('pino')
ESM import, but CommonJS require works in Node < 16. Package is ESM-only from v3? (check)
transport
import { pino } from 'pino'; const transport = pino.transport({ target: 'pino-mongodb', options: {...} })
const transport = require('pino-mongodb')({...})
For modern Pino transport, use pino.transport. Legacy require pattern is still supported but deprecated.
pino-mongodb CLI
echo '{"msg":"hello"}' | npx pino-mongodb --collection logs mongodb://localhost:27017/mydb
pino-mongodb --help
CLI usage reads from stdin; does not require Node code.

Sets up Pino logger with MongoDB transport and logs a sample message. Also shows alternative CLI usage.

import pino from 'pino'; const transport = pino.transport({ target: 'pino-mongodb', level: 'info', options: { uri: process.env.MONGO_URI ?? 'mongodb://localhost:27017/', database: 'logs', collection: 'app-logs', mongoOptions: { auth: { username: process.env.MONGO_USER ?? '', password: process.env.MONGO_PASS ?? '' } } } }); const logger = pino(transport); logger.info({ user: 'Alice', action: 'login' }, 'User logged in'); // For CLI usage: // echo '{"msg":"hello"}' | pino-mongodb --collection logs mongodb://localhost:27017/mydb
Debug
Known issues
gotchaMongoDB does not accept keys starting with '$' (e.g., $and). Log objects containing such keys will fail to insert. Wrap them under a property like 'query'.
fix
Use parseLine option to transform log objects: opts.parseLine = (str) => { const obj = JSON.parse(str); // wrap problematic keys }
affects: >=1.0.0
deprecatedLegacy transport interface (using require('pino-mongodb') directly) is deprecated in favor of pino.transport.
fix
Use pino.transport({ target: 'pino-mongodb', options: { ... } })
affects: >=5.0.0
gotchaMissing logs: if the log object contains properties that conflict with MongoDB reserved fields (e.g., _id), they may be overwritten or cause errors.
fix
Avoid using reserved keys or use parseLine to rename them.
affects: >=1.0.0
Errors
Common errors & fixes
MongoServerError: Document contains invalid key: $and
Log object has a key starting with '$' which is not allowed by MongoDB.
fix
Wrap such keys under a nested property, e.g., { query: { $and: [...] } }. Use parseLine option to transform logs.
TypeError: pino.transport is not a function
Using older version of Pino (<7) that does not support pino.transport.
fix
Upgrade to pino@7 or later, or use legacy require pattern.
Error: Cannot find module 'pino-mongodb'
Package not installed or CommonJS require used when package is ESM-only.
fix
Install the package: npm install pino-mongodb. If using ESM, use import syntax.
Upgrade
Version history
5.0.0latest on npm
Audit
Dependencies
pinorequiredUsed as a transport for Pino logger; required for programmatic usage.
mongodbrequiredMongoDB Node.js driver for database operations.
Agent activity
15 hits · last 30 days
node
14
Resources
pino-mongodb — npm install pino-mongodb · libregistry