Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including egg-mongo-native with all npm packages installed. Try it out:

egg-mongo-native lists no main file and has no index.js, so it can't be directly required. If this is a mistake, please let us know. It may however contain internal files that you can require manually:

// require("egg-mongo-native/[??]")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

egg-mongo-native v3.5.0

MongoDB egg.js plugin using native driver.

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

中文版

Users 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

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free