eggjs使用egg-mongoose操作MongoDB数据库
npm install egg-mongoose --save
// config/plugin.js
module.exports = {
...
...
mongoose: {
enable: true,
package: 'egg-mongoose'
}
}
// config/config.default.js
module.exports = appInfo => {
const config = exports = {
};
...
...
config.mongoose = {
url: 'mongodb://localhost:27017',
options: {
user: 'xxxx', // 数据库用户名
pass: 'xxxx', // 数据库密码
dbName: 'PieCake' // 数据库名
}
}
}
// app/model/userModel.js
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const PostSchema = new Schema(