# pino-mongodb

> Insert JSON from stdin into MongoDB

Latest version **5.0.0** (published 2025-11-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install pino-mongodb
pnpm add pino-mongodb
yarn add pino-mongodb
bun add pino-mongodb
```

Provides the command `pino-mongodb`.

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2025-11-20 |
| First published | 2016-11-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 10.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 59 |
| Author | Viktor Kuroljov |
| Maintainers | matteo.collina, jsumners, watson, viktor.konsta |
| Keywords | pino, mongo, mongodb, pino-mongodb, logs, logger |

## Links

- npm: https://www.npmjs.com/package/pino-mongodb
- Repository: https://github.com/pinojs/pino-mongodb
- Issues: https://github.com/pinojs/pino-mongodb/issues
- npm.io page: https://npm.io/package/pino-mongodb

## Dependencies (5)

- [muri](https://npm.io/package/muri.md) ^1.3
- [carrier](https://npm.io/package/carrier.md) ^0.3
- [mongodb](https://npm.io/package/mongodb.md) ^6.20.0
- [commander](https://npm.io/package/commander.md) ^14.0.2
- [pino-abstract-transport](https://npm.io/package/pino-abstract-transport.md) ^3.0.0

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [btrz-health-check](https://npm.io/package/btrz-health-check.md) — 474 weekly downloads

## Recent versions

- 5.0.0 (latest) — 2025-11-20
- 4.3.0 — 2022-09-20
- 4.2.0 — 2022-08-27
- 4.1.0 — 2022-08-06
- 4.0.1 — 2022-06-30
- 4.0.0 — 2022-06-30
- 3.1.1 — 2021-10-04
- 3.1.0 — 2021-09-23
- 3.0.1 — 2021-09-21
- 3.0.0 — 2021-09-15
- 2.1.2 — 2020-09-16
- 2.1.1 — 2020-09-16
- 2.1.0 — 2020-09-16
- 2.0.1 — 2018-11-29
- 2.0.0 — 2018-11-19
- … 11 more at https://npm.io/package/pino-mongodb/versions

## README

# pino-mongodb
[![npm version](https://img.shields.io/npm/v/pino-mongodb)](https://www.npmjs.com/package/pino-mongodb)
[![Build Status](https://img.shields.io/github/workflow/status/pinojs/pino-mongodb/CI)](https://github.com/pinojs/pino-mongodb/actions)

> Insert JSON from stdin into MongoDB

This project is part of the `pino` logger family, however you can use it to parse and insert any
`JSON` into the `mongo`.

## Install

```bash
$ npm i pino-mongodb
```

## Usage as Pino Transport

You can use this module as a [pino transport](https://getpino.io/#/docs/transports?id=v7-transports) like so:

```js
const pino = require('pino')
const transport = pino.transport({
  target: 'pino-mongodb',
  level: 'info',
  options: {
    uri: 'mongodb://localhost:27017/',
    database: 'logs',
    collection: 'log-collection',
    mongoOptions: {
      auth: {
        username: 'one',
        password: 'two'
      }
    }
  }
})

pino(transport)
```

The `mongoOptions` is provided to the the standard mongodb client. All the available options are described on [its official documentation](https://mongodb.github.io/node-mongodb-native/4.1/interfaces/MongoClientOptions.html).

Note that you may encouter missing logs in special cases: it dependes on data and mongo's version. Please checkout the [mongodb limitation](https://docs.mongodb.com/manual/reference/limits/) official documentation.  
For example on MongoDB 4:

```js
// IT DOES NOT WORK:
log.info({ $and: [{ a: 1 }, { b: 2 }] }, 'my query is')

// IT WORKS:
log.info({ query: { $and: [{ a: 1 }, { b: 2 }]} }, 'my query is')
```

If you want a custom parser to handle the above case. You need to wrap `pino-mongo` and pass a function through `option.parseLine`. Any value that is not a function will be ignored in this option.

```js
// mongo-transport.js
'use strict'

const transport = require('pino-mongodb')

module.exports = function(opts) {
  opts.parseLine = function(str) { // `str` is passed from `pino` and expected to be a string
    const obj = JSON.parse(str)
    
    // do anything you want...

    return obj // return value is expected to be a json that will pass and save inside mongodb
  }
  return transport(opts)
}

// main.js
const pino = require('pino')
const transport = pino.transport({
  target: './mongo-transport.js',
  uri: 'mongodb://localhost:27017/logs',
  collection: 'log-collection',
})
pino(transport)
```

## Usage as Pino Legacy Transport

Pino supports a [legacy transport interface](https://getpino.io/#/docs/transports?id=legacy-transports)
that is still supported by this module.

### Get started

```bash
$ echo '{"name": "Viktor"}' | pino-mongodb [options] [mongo-url]
```

```bash
$ cat many.logs | pino-mongodb [options] [mongo-url]
```

```bash
$ node ./app.js | pino-mongodb [options] [mongo-url]
```

### CLI Options

```
Usage: pino-mongodb [options] [mongo-url]

Insert JSON from stdin into MongoDB

Options:
  -V, --version            output the version number
  -c, --collection <name>  database collection (default: "logs")
  -o, --stdout             output inserted documents into stdout (default:
                           false)
  -e, --errors             output insertion errors into stderr (default: false)
  -h, --help               display help for command
```

## Tests

To run unit tests:

```bash
$ npm t
```

To run integrational tests with real mongo server:

```bash
$ npm run trial
```

Note, you will have to have `docker` and `docker-compose` installed
on your machine for that!

## License

Licensed under [MIT](./LICENSE).

---
_Source: https://npm.io/package/pino-mongodb · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
