{"id":13836117,"url":"https://github.com/fastify/fastify-mongodb","last_synced_at":"2026-01-26T20:17:09.387Z","repository":{"id":19501701,"uuid":"87167477","full_name":"fastify/fastify-mongodb","owner":"fastify","description":"Fastify MongoDB connection plugin","archived":false,"fork":false,"pushed_at":"2025-05-01T04:41:14.000Z","size":171,"stargazers_count":259,"open_issues_count":1,"forks_count":52,"subscribers_count":18,"default_branch":"main","last_synced_at":"2025-05-01T05:26:27.536Z","etag":null,"topics":["database","fastify","fastify-plugin","mongodb"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@fastify/mongodb","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastify.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"fastify","open_collective":"fastify"}},"created_at":"2017-04-04T09:12:49.000Z","updated_at":"2025-05-01T04:41:11.000Z","dependencies_parsed_at":"2023-01-13T20:24:41.866Z","dependency_job_id":"cb5f49a4-e60e-42d0-8301-577a638cf8b9","html_url":"https://github.com/fastify/fastify-mongodb","commit_stats":{"total_commits":209,"total_committers":34,"mean_commits":6.147058823529412,"dds":0.7511961722488039,"last_synced_commit":"c25f212d55fe3befc07ef0b1a6f02640c339b730"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-mongodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-mongodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-mongodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-mongodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastify","download_url":"https://codeload.github.com/fastify/fastify-mongodb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254254041,"owners_count":22039792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","fastify","fastify-plugin","mongodb"],"created_at":"2024-08-04T15:00:35.894Z","updated_at":"2026-01-26T20:17:09.360Z","avatar_url":"https://github.com/fastify.png","language":"JavaScript","readme":"# @fastify/mongodb\n\n[![CI](https://github.com/fastify/fastify-mongodb/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-mongodb/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@fastify/mongodb.svg?style=flat)](https://www.npmjs.com/package/@fastify/mongodb)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nFastify MongoDB connection plugin; with this you can share the same MongoDB connection pool in every part of your server.\n\nUnder the hood the official [MongoDB](https://github.com/mongodb/node-mongodb-native) driver is used,\nthe options that you pass to `register` will be passed to the Mongo client.\nThe `mongodb` driver is v7.x.x.\n\nIf you do not provide the client by yourself (see below), the URL option is *required*.\n\n## Install\n\n```bash\nnpm i @fastify/mongodb\n```\n\n## Usage\n\nAdd it to your project with `register` and you are done!\n\n```js\nconst fastify = require('fastify')()\n\nfastify.register(require('@fastify/mongodb'), {\n  // force to close the mongodb connection when app stopped\n  // the default value is false\n  forceClose: true,\n\n  url: 'mongodb://mongo/mydb'\n})\n\nfastify.get('/user/:id', async function (req, reply) {\n  // Or this.mongo.client.db('mydb').collection('users')\n  const users = this.mongo.db.collection('users')\n\n  // if the id is an ObjectId format, you need to create a new ObjectId\n  const id = new this.mongo.ObjectId(req.params.id)\n  try {\n    const user = await users.findOne({ id })\n    return user\n  } catch (err) {\n    return err\n  }\n})\n\nfastify.listen({ port: 3000 }, err =\u003e {\n  if (err) throw err\n})\n```\n\nYou may also supply a pre-configured instance of `mongodb.MongoClient`:\n\n```js\nconst mongodb = require('mongodb')\nmongodb.MongoClient.connect('mongodb://mongo/db')\n  .then((client) =\u003e {\n    const fastify = require('fastify')()\n\n    fastify.register(require('@fastify/mongodb'), { client: client })\n      .register(function (fastify, opts, next) {\n        const db = fastify.mongo.client.db('mydb')\n        // ...\n        // ...\n        // ...\n        next()\n      })\n  })\n  .catch((err) =\u003e {\n    throw err\n  })\n```\n\nNotes:\n\n* the passed `client` connection will **not** be closed when the Fastify server\nshuts down.\n* to terminate the MongoDB connection you have to manually call the [fastify.close](https://fastify.dev/docs/latest/Reference/Server/#close) method (for example for testing purposes, otherwise the test will hang).\n* `mongodb` connection timeout is reduced from 30s (default) to 7.5s to throw an error before `fastify` plugin timeout.\n\n## Reference\n\nThis plugin decorates the `fastify` instance with a `mongo` object. That object has the\nfollowing properties:\n\n* `client` is the [`MongoClient` instance](http://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html)\n* `ObjectId` is the [`ObjectId` class](http://mongodb.github.io/node-mongodb-native/3.3/api/ObjectID.html)\n* `db` is the [`DB` instance](http://mongodb.github.io/node-mongodb-native/3.3/api/Db.html)\n\nThe `ObjectId` class can also be directly imported from the plugin as it gets re-exported from `mongodb`:\n\n```js\nconst { ObjectId } = require('@fastify/mongodb')\n\nconst id = new ObjectId('some-id-here')\n```\n\nThe `db` property is added **only if**:\n\n* a `database` string option is given during the plugin registration.\n* the connection string contains the database name. See the [Connection String URI Format](https://docs.mongodb.com/manual/reference/connection-string/#connection-string-uri-format)\n\nA `name` option can be used to connect to multiple MongoDB clusters.\n\n```js\nconst fastify = require('fastify')()\n\nfastify\n  .register(require('@fastify/mongodb'), { url: 'mongodb://mongo1/mydb', name: 'MONGO1' })\n  .register(require('@fastify/mongodb'), { url: 'mongodb://mongo2/otherdb', name: 'MONGO2' })\n\nfastify.get('/', function (req, res) {\n  // This collection comes from \"mongodb://mongo1/mydb\"\n  const coll1 = this.mongo.MONGO1.db.collection('my_collection')\n  // This collection comes from \"mongodb://mongo2/otherdb\"\n  const coll2 = this.mongo.MONGO2.db.collection('my_collection')\n  // ...\n  // ...\n  // do your stuff here\n  // ...\n  // ...\n  res.send(yourResult)\n})\n```\n\n## Acknowledgments\n\nThis project is kindly sponsored by:\n\n* [nearForm](https://nearform.com)\n* [LetzDoIt](https://www.letzdoitapp.com/)\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","funding_links":["https://github.com/sponsors/fastify","https://opencollective.com/fastify"],"categories":["\u003ch2 align=\"center\"\u003eAwesome Fastify\u003c/h2\u003e","JavaScript"],"sub_categories":["\u003ch2 align=\"center\"\u003eEcosystem\u003c/h2\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastify%2Ffastify-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-mongodb/lists"}