Skip to content
MongoDB egg.js plugin using native driver.
Branch: master
Clone or download
Latest commit 8f7c1f8 Feb 2, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github init Jul 2, 2017
.vscode release: 3.0.0 Jan 23, 2018
config
test refactor: update deps (#33) Feb 2, 2019
.eslintignore init Jul 2, 2017
.eslintrc init Jul 2, 2017
.gitignore refactor: integrate easy-mongodb (#31) Jan 24, 2019
.travis.yml release: 3.0.0 Mar 12, 2018
LICENSE init Jul 2, 2017
README.md refactor: integrate easy-mongodb (#31) Jan 24, 2019
README.zh_CN.md refactor: integrate easy-mongodb (#31) Jan 24, 2019
app.js refactor: update deps (#33) Feb 2, 2019
index.d.ts refactor: integrate easy-mongodb (#31) Jan 24, 2019
package.json release: 3.4.3 Feb 2, 2019
tsconfig.json refactor: integrate easy-mongodb (#31) Jan 24, 2019

README.md

NPM version NPM quality build status Test coverage David deps Known Vulnerabilities npm download

中文版

Uses who don't use Egg.js could use easy-mongodb.

This plugin base on node-mongodb-native, provides the official MongoDB native driver and APIs.

It wraps some frequently-used API to make it easy to use but keep all properties as it is. For example, to find a document you need this with official API

db.collection('name')
  .find(query, options)
  .skip(skip)
  .limit(limit)
  .project(project)
  .sort(sort)
  .toArray();

and with this plugin

app.mongo.find('name', { query, skip, limit, project, sort, options });

Install

$ npm i egg-mongo-native --save

Enable Plugin

// {app_root}/config/plugin.js
exports.mongo = {
  enable: true,
  package: 'egg-mongo-native',
};

Configuration

Single Instance

// {app_root}/config/config.default.js
exports.mongo = {
  client: {
    host: 'host',
    port: 'port',
    name: 'test',
    user: 'user',
    password: 'password',
    options: {},
  },
};

Replica Set (v2.1.0 or higher)

// mongodb://host1:port1,host2:port2/name?replicaSet=test
exports.mongo = {
  client: {
    host: 'host1,host2',
    port: 'port1,port2',
    name: 'name',
    options: {
      replicaSet: 'test',
    },
  },
};

// mongodb://host:port1,host:port2/name?replicaSet=test
exports.mongo = {
  client: {
    host: 'host', // or ['host']
    port: 'port1,port2', // or ['port1', 'port2']
    name: 'name',
    options: {
      replicaSet: 'test',
    },
  },
};

Multiple Instances

Can not set client and clients both.

// {app_root}/config/config.default.js
exports.mongo = {
  clients: {
    db1: {
      host: 'host',
      port: 'port',
      name: 'db1',
      user: 'user',
      password: 'password',
      options: {},
    },
    db2: {
      host: 'host',
      port: 'port',
      name: 'db2',
      user: 'user',
      password: 'password',
      options: {},
    },
  },
};

see config/config.default.js for more detail.

Example

The APIs provided by plugin usually need two arguments. The first is commonly the collection name, and the second is an object keeps the arguments of official API. For example, to insert one document using official API

db.collection('name').insertOne(doc, options);

and using plugin API

const args = { doc, options };
app.mongo.insertOne('name', args);

For Multiple Instances

const args = { doc, options };
app.mongo.get('db1').insertOne('name', args);

The args is an object provides the arguments to official API.

Please read easy-mongodb for all APIs(tansaction is now supported) and more examples.

License

MIT

You can’t perform that action at this time.