{"id":25096650,"url":"https://github.com/brickyang/easy-mongodb","last_synced_at":"2025-04-17T12:42:39.825Z","repository":{"id":143736042,"uuid":"146160195","full_name":"brickyang/easy-mongodb","owner":"brickyang","description":null,"archived":false,"fork":false,"pushed_at":"2019-09-21T06:40:55.000Z","size":29,"stargazers_count":4,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-02T00:37:24.367Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/brickyang.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}},"created_at":"2018-08-26T07:23:07.000Z","updated_at":"2020-07-03T08:28:38.000Z","dependencies_parsed_at":"2023-07-03T17:32:22.677Z","dependency_job_id":null,"html_url":"https://github.com/brickyang/easy-mongodb","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brickyang%2Feasy-mongodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brickyang%2Feasy-mongodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brickyang%2Feasy-mongodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brickyang%2Feasy-mongodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brickyang","download_url":"https://codeload.github.com/brickyang/easy-mongodb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625499,"owners_count":21135513,"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":[],"created_at":"2025-02-07T16:49:22.730Z","updated_at":"2025-04-17T12:42:39.809Z","avatar_url":"https://github.com/brickyang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM version][npm-image]][npm-url]\n[![NPM quality][quality-image]][quality-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![David deps][david-image]][david-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/@brickyang/easy-mongodb.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@brickyang/easy-mongodb\n[quality-image]: http://npm.packagequality.com/shield/@brickyang/easy-mongodb.svg?style=flat-square\n[quality-url]: http://packagequality.com/#?package=@brickyang/easy-mongodb\n[travis-image]: https://img.shields.io/travis-ci/brickyang/easy-mongodb.svg?style=flat-square\n[travis-url]: https://travis-ci.org/brickyang/easy-mongodb\n[codecov-image]: https://img.shields.io/codecov/c/github/brickyang/easy-mongodb.svg?style=flat-square\n[codecov-url]: https://codecov.io/github/brickyang/easy-mongodb?branch=master\n[david-image]: https://david-dm.org/brickyang/easy-mongodb/status.svg?style=flat-square\n[david-url]: https://david-dm.org/brickyang/easy-mongodb?branch=master\n[snyk-image]: https://snyk.io/test/npm/@brickyang/easy-mongodb/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/@brickyang/easy-mongodb\n[download-image]: https://img.shields.io/npm/dm/@brickyang/easy-mongodb.svg?style=flat-square\n[download-url]: https://npmjs.org/package/@brickyang/easy-mongodb\n\n[**中文版**](https://github.com/brickyang/easy-mongodb/blob/master/README.zh_CN.md)\n\nThis lib base on\n[node-mongodb-native](https://github.com/mongodb/node-mongodb-native), provides\nthe official MongoDB native driver and APIs.\n\nIt wraps some frequently-used API to make it easy to use but keep all properties\nas it is. For example, to find a document you need this with official API\n\n```js\ndb.collection('name')\n  .find(query, options)\n  .skip(skip)\n  .limit(limit)\n  .project(project)\n  .sort(sort)\n  .toArray();\n```\n\nand with this lib\n\n```js\nmongo.find('name', { query, skip, limit, project, sort, options });\n```\n\nIf you are using Egg.js, please see [egg-mongo-native](https://github.com/brickyang/egg-mongo-native).\n\n## Installation\n\n```bash\nnpm install --save @brickyang/easy-mongodb\n```\n\n## Configuration\n\n### Single\n\n```js\nconst config = {\n  host: 'host',\n  port: 'port',\n  name: 'test',\n  user: 'user',\n  password: 'password',\n  options: {},\n};\n```\n\n### Replica Set\n\n```js\n// mongodb://host1:port1,host2:port2/name?replicaSet=test\nconst config = {\n  host: 'host1,host2',\n  port: 'port1,port2',\n  name: 'name',\n  options: { replicaSet: 'test' },\n};\n\n// mongodb://host:port1,host:port2/name?replicaSet=test\nconst config = {\n  host: 'host',\n  port: 'port1,port2',\n  name: 'name',\n  options: { replicaSet: 'test' },\n};\n```\n\n## Usage\n\nThe APIs provided by this lib usually need two arguments. The first is commonly\nthe collection name, and the second is an object keeps the arguments of official\nAPI.\n\n```js\n// TypeScript\n// import MongoDB from '@brickyang/easy-mongodb';\n\nconst MongoDB = require('@brickyang/easy-mongodb').default;\n\nconst mongo = new MongoDB(config);\n\n// connection\nmongo\n  .connect()\n  .then(client =\u003e {\n    // `client` is instance of connected MongoClient\n  })\n  .catch(error =\u003e {\n    // handle error\n  });\n\n// or\n\nmongo.on('connect', () =\u003e {\n  // do something\n});\n\nmongo.on('error', error =\u003e {\n  // handle error\n});\n\n// insert one doc\nconst args = { doc, options };\nmongo.insertOne('collection', args);\n\n// transaction\nconst session = mongo.startTransaction();\nconst args = { doc, { session } };\nmongo.insertOne('collection1', args);\nmongo.insertOne('collection2', args);\nsession.commitTransaction();\n```\n\n## Members\n\n- **db**: Db instance\n- **client**: MongoClient instance\n- **featureCompatibilityVersion**: Transaction need '4.0' or above\n\n## API\n\nUntil now, this plugin provides these functions:\n\n- **connect**\n- **insertOne**\n- **insertMany**\n- **findOne**\n- **findOneAndUpdate**\n- **findOneAndReplace**\n- **findOneAndDelete**\n- **updateMany**\n- **deleteMany**\n- **find**\n- **count**: 已过时\n- **countDocuments**\n- **estimatedDocumentCount**\n- **distinct**\n- **createIndex**\n- **listCollection**\n- **createCollection**\n- **aggregate**\n- **startSession**\n- **startTransaction**\n\nYou can always use `mongo.db` and `mongo.client` to use all official APIs. Check the\nAPIs here:\n[Node.js MongoDB Driver API](https://mongodb.github.io/node-mongodb-native/3.1/api/).\n\n## Promise\n\n```js\nfunction create(doc) {\n  mongo\n    .insertOne('name', { doc })\n    .then(result =\u003e console.log(result))\n    .catch(error =\u003e console.error(error));\n}\n```\n\n## Async/Await\n\n```js\nasync function create(doc) {\n  try {\n    const result = await mongo.insertOne('name', { doc });\n    console.log(result);\n  } catch (error) {\n    console.error(error);\n  }\n}\n```\n\nIf you use `mongo.db` you could use callback(usually the last argument), but\nthis lib doesn't support callback because Promise and async/await are better.\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrickyang%2Feasy-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrickyang%2Feasy-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrickyang%2Feasy-mongodb/lists"}