红联Linux门户
Linux帮助

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

发布时间:2016-11-09 18:25:26来源:红联作者:wemallshop
koa-mongo
MongoDB middleware for koa, support connection pool.
koa-mongo
koa-mongo is a mongodb middleware for koa, support connection pool.
Install[code]npm i koa-mongo --save[/code]Usage[code]app.use(mongo({
host: 'localhost',
port: 27017,
user: 'admin',
pass: '123456',
db: 'test',
max: 100,
min: 1,
timeout: 30000,
log: false
}));[/code]or[code]app.use(mongo({
uri: 'mongodb://admin:[email protected]:27017/test', //or url
max: 100,
min: 1,
timeout: 30000,
log: false
}));[/code]Example[code]'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);[/code]License

MIT
wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统
wemall地址:http://www.wemallshop.com
代码地址:http://js.koahub.com/home/feature/koa-mongo
文章评论

共有 1 条评论

  1. duanshupeng 于 2017-06-21 10:38:16发表:

    66