首先来个小例子

//1.引入模板
let mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/m_data');

//监听各种状态
let db = mongoose.connection;
db.on("error",()=>{
    console.log("连接失败")
});

db.once('open',function(){
    console.log("连接成功")    
})


db.once('close',function(){
    console.log("数据库断开成功")    
})

//2创建Schema(模式对象)
let Schema = mongoose.Schema;//定义规则
let personSchema = new Schema({
    name:String,
    age:Number,
    sex:{
        type:String,
        default:"男"
    },
    chat:String
});

//3.创建Model对象
let personModel = mongoose.model("person",personSchema);

//4.插入文档
personModel.create({
    name:"姐姐",
    age:19,
    sex:"女",
    chat:"hahaha"
},(err)=>{
    if(!err){
        console.log("插入成功");
    }else{
        throw err;
    }
})
personModel.create({
    name:"可好看的姐姐呢",
    age:19,
    sex:"女",
    chat:"hahaha"
},(err)=>{
    if(!err){
        console.log("插入成功");
    }else{
        throw err;
    }
})

然后来个大例子

//1.引入模板
let mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/m_data',{useNewUrlParser:true});

//监听各种状态
let db = mongoose.connection;
db.on("error",()=>{
    console.log("连接失败")
});

db.once('open',function(){
    console.log("连接成功")    
})


db.once('close',function(){
    console.log("数据库断开成功")    
})

//2创建Schema(模式对象)
let Schema = mongoose.Schema;//定义规则
let personSchema = new Schema({
    name:String,
    age:Number,
    sex:{
        type:String,
        default:"男"
    },
    chat:String
});

//3.创建Model对象
let personModel = mongoose.model("person",personSchema);

//4.增删改查

//4.1增加
// personModel.create([
//     { name:"可好看的姐姐0呢", age:190, sex:"女", chat:"hahaha"},
//     { name:"可好看的姐姐1呢", age:191, sex:"女", chat:"hahaha"},
//     { name:"可好看的姐姐2呢", age:192, sex:"女", chat:"hahaha"},
//     { name:"可好看的姐姐2呢", age:193, sex:"女", chat:"hahaha"}
// ],(err)=>{
//     if(!err){
//         console.log("插入成功");
//     }else{
//         throw err;
//     }
// })

//4.2查找

// personModel.find({},(err,docs)=>{ //{}=>全部
//     if(!err)
//     {
//         console.log(docs);
//         console.log(typeof docs);
//     }
// });

// personModel.find({name:"姐姐"},{name:1},(err,docs)=>{  //查找所有文档把name返回回来
//     if(!err)
//     {
//         console.log(docs);
//         console.log(typeof docs);
//     }
//     else{
//         console.log("找寻失败");
        
//     }
// });

// personModel.find({},"-_id name sex",{skip:5,limit:5},(err,docs)=>{  //建议还是用大括号的形式
//     if(!err)
//     {
//         console.log(docs);
//         console.log(typeof docs);
//     }
//     else{
//         console.log("找寻失败");
        
//     }
// });


//4.3 修改
// personModel.updateOne({name:"可好看的姐姐呢"},{$set:{age:20}},(err)=>{
//     if(!err)
//     {
//         console.log("修改成功");
//     }
//     else{
//         console.log("修改失败");
//     }
// })

//4.4删除
//Model.deleteMany()
//Model.deleteOne()
//Model.remove()

// personModel.deleteOne({name:"可好看的姐姐呢"},(err)=>{
//     if(!err){
//         console.log("删除成功");   
//     }else{
//         throw err;
//     }
// })

//4.5 统计文档个数
//Model.count();

personModel.countDocuments({},(err,count)=>{
    if(!err)
    {
        console.log(count);
    }
})


//2创建Schema(模式对象)
let Schema = mongoose.Schema;//定义规则
let personSchema = new Schema({
    name:String,
    age:Number,
    sex:{
        type:String,
        default:"男"
    },
    chat:String
});

//3.创建Model对象
let personModel = mongoose.model("person",personSchema);

let person = new personModel({
    name:"姐姐",
    age:19,
    sex:"女",
    chat:"all"
});
person.save((err,product)=>{
    if(!err){
        console.log(product);
    }
});

基于mongoose实现分页,排序

router.get("/list",(req,res,next)=>{
   //分页功能实现
    let page = parseInt(req.param("page"));
    let pageSize = parseInt(req.param("pageSize"));
    let sort = parseInt(req.param("sort"));//-1降序,1升序
    let priceLevel = req.param("priceLevel");
    let skip = (page-1)*pageSize;

    let params = {};
    let priceGt = '',priceLte = '';
    if(priceLevel!="all")
    {
        switch (priceLevel){
            case '0':priceGt = 0;priceLte = 1000;break;
            case '1':priceGt = 1000;priceLte = 2000;break;
            case '2':priceGt = 2000;priceLte = 5000;break;
        }
        params = {//实现分价格区间展示数据
            salePrice:{
                $gt:priceGt,
                $lte:priceLte
            }
        }
    }
    
    let goodsModel = Goods.find(params).skip(skip).limit(pageSize);//实现分页展示数据即每页最多
    goodsModel.sort({'salePrice':sort});
    goodsModel.exec((err,doc)=>{
        if(err){
            res.json({
                status:"1",
                msg:err.message
            });
        }else{
            res.json({
                status:"0",
                msg:"",
                res:{
                    count:doc.length,
                    list:doc
                }
            })
        }
    })