1、Mongoose索引html
索引是对数据库表中一列或多列的值进行排序的一种结构, 能够让咱们查询数据库变得更快。 MongoDB 的索引几乎与传统的关系型数据库如出一辙, 这其中也包括一些基本的查询优化技巧。 数据库
var DeviceSchema = new mongoose.Schema({ sn: { type: Number, // 惟一索引 unique: true }, name: { type: String, // 普通索引 index: true } });
2、Mongoose内置CURDmongoose
参考:https://mongoosejs.com/docs/queries.html
优化
3、Mongoose扩展CURD静态方法和实例方法ui
var mongoose=require('./db.js'); var UserSchema=mongoose.Schema({ name:{ type:String }, age:Number,
status:{ type:Number, default:1 } }) // 静态方法 UserSchema.statics.findByUid=function(uid,cb){ this.find({"_id":uid},function(err,docs){ cb(err,docs); }) }
// 实例方法 UserSchema.methods.print = function(){ console.log('这是一个实例方法'); console.log(this); }; module.exports=mongoose.model('User',UserSchema,'user');