一、Mongoose索引
索引是对数据库表中一列或多列的值进行排序的一种结构, 可以让我们查询数据库变得更快。 MongoDB 的索引几乎与传统的关系型数据库一模一样, 这其中也包括一些基本的查询优化技巧。
var DeviceSchema = new mongoose.Schema({
sn: {
type: Number,
// 唯一索引
unique: true
},
name: {
type: String,
// 普通索引
index: true
}
});
二、Mongoose内置CURD
参考:https://mongoosejs.com/docs/queries.html
三、Mongoose扩展CURD静态方法和实例方法
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');