1.mongoose默认参数
var UserSchema = new mongoose.Schema({
name:String,
age:Number,
status:{
type: Number,
default:1
}
})
2.模块化
//model/db.js
const mongoose = require('mongoose');
mongoose.connect( 'mongodb://127.0.0.1:27017/eggcms', {useNewUrlParser: true},(err)=>{
if(err) throw err;
console.log("database连接成功")
});
module.exports = mongoose;
//model/user.js
var mongoose = require('./db');
var UserSchema = new mongoose.Schema({
name:String,
age:Number
})
var User = mongoose.model('User',UserSchema,'user');
module.exports = User;
//app.js引入
const UserModel = require('./model/user');
UserModel.find({},(err,doc)=>{
console.log(doc)
})
3.console.time测试mongoose的性能
console.time('user')
const UserModel = require('./model/user');
console.timeEnd('user')
console.time('news')
const NewsModel = require('./model/news');
console.timeEnd('news')