KoaHub平台基于Node.js开发的Koa的连接MongoDB插件代码详情


发布者 wemallshop  发布时间 1478663465645
关键字 Node.JS 

koa-mongo

MongoDB middleware for koa, support connection pool.

koa-mongo

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

Install

npm i koa-mongo --save

 

Usage

app.use(mongo({
  host: 'localhost',
  port: 27017,
  user: 'admin',
  pass: '123456',
  db: 'test',
  max: 100,
  min: 1,
  timeout: 30000,
  log: false
}));

 

or

app.use(mongo({
  uri: 'mongodb://admin:123456@localhost:27017/test', //or url
  max: 100,
  min: 1,
  timeout: 30000,
  log: false
}));

 

Example

'use strict';
 
var koa = require('koa');
var mongo = require('koa-mongo');
 
var app = koa();
 
app.use(mongo());
app.use(function* (next) {
  yield this.mongo.db('test').collection('users').insert({ name: 'haha' });
  this.body = yield this.mongo.db('test').collection('users').findOne();
  this.mongo.db('test').collection('users').remove().then(function (res) {
    console.log(res.result);
  });
});
app.listen(3000);

 

License

MIT

wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统

 









  开源的 OurJS
OurJS开源博客已经迁移到 OnceOA 平台。

  关注我们
扫一扫即可关注我们:
OnceJS

OnceOA