# koa-mongo

> MongoDB middleware for koa, support connection pool.

Latest version **1.9.3** (published 2021-01-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-mongo
pnpm add koa-mongo
yarn add koa-mongo
bun add koa-mongo
```

## Health

**Score 18/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.9.3 |
| Published | 2021-01-18 |
| First published | 2014-08-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/koa-mongo) |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 142 |
| Author | nswbmw |
| Maintainers | nswbmw |
| Keywords | koa, mongo, mongodb |

## Links

- npm: https://www.npmjs.com/package/koa-mongo
- Repository: https://github.com/nswbmw/koa-mongo
- Homepage: https://github.com/nswbmw/koa-mongo#readme
- Issues: https://github.com/nswbmw/koa-mongo/issues
- npm.io page: https://npm.io/package/koa-mongo

## Dependencies (4)

- [muri](https://npm.io/package/muri.md) 1.3.0
- [debug](https://npm.io/package/debug.md) 4.3.1
- [mongodb](https://npm.io/package/mongodb.md) 3.6.3
- [generic-pool](https://npm.io/package/generic-pool.md) 3.7.1

## 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
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.9.3 (latest) — 2021-01-18
- 1.9.2 — 2020-09-09
- 1.9.1 — 2019-12-13
- 1.9.0 — 2019-10-31
- 1.8.2 — 2019-09-27
- 1.8.1 — 2019-05-09
- 1.8.0 — 2019-04-01
- 1.7.0 — 2019-03-27
- 1.6.0 — 2019-01-03
- 1.5.0 — 2018-11-13
- 1.4.1 — 2018-08-17
- 1.4.0 — 2018-08-10
- 1.3.0 — 2017-12-15
- 0.8.0 — 2017-10-05
- 1.2.1 — 2017-07-11
- … 15 more at https://npm.io/package/koa-mongo/versions

## README

## koa-mongo

koa-mongo is a mongodb middleware for koa@2, support connection pool.

### Install

```sh
npm i koa-mongo --save
```

### Usage

```js
app.use(mongo({
  host: 'localhost',
  port: 27017,
  user: 'admin',
  pass: '123456',
  db: 'test',
  authSource: 'admin',
  max: 100,
  min: 1,
  ...
}));
```

or

```js
app.use(mongo({
  uri: 'mongodb://admin:123456@localhost:27017/test?authSource=admin', //or url
  max: 100,
  min: 1
  ...
}));
```

or

```js
app.use(mongo({
  uri: 'mongodb://admin:123456@localhost:27017/test?authSource=admin', //or url
  max: 100,
  min: 1
  ...
}, {
  useUnifiedTopology: true
  ...
}
));
```

defaultOptions:

```js
{
  host: 'localhost',
  port: 27017,
  db: 'test',
  authSource: 'admin',
  max: 100,
  min: 1,
  acquireTimeoutMillis: 100
}
```

More options see [generic-pool](https://github.com/coopernurse/node-pool).

### ctx.mongo & ctx.db

`ctx.mongo` is an instance of MongoClient.

```js
ctx.db === ctx.mongo.db(dbName)
```

### Example

```js
const Koa = require('koa')
const mongo = require('koa-mongo')

const app = new Koa()

app.use(mongo())
app.use(async (ctx, next) => {
  // ctx.db === ctx.mongo.db('test')
  const result = await ctx.db.collection('users').insert({ name: 'haha' })
  const userId = result.ops[0]._id.toString()
  ctx.body = await ctx.db.collection('users').find().toArray()
  ctx.db.collection('users').remove({
    _id: mongo.ObjectId(userId)
  })
})
app.listen(3000, () => {
  console.log('listening on port 3000')
})
```

### [Mongolass](https://github.com/mongolass/mongolass)

Mongolass is a elegant MongoDB driver for Node.js.

### License

MIT

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