mongoose---分页查询

app.js

const express = require('express');
const app = express();
const fs = require('fs')
const path = require('path')
const favicon = require('serve-favicon');
const bodyParser = require('body-parser')
const multiparty = require('multiparty');
const cookieParser = require('cookie-parser')
const session = require('express-session');

app.post('/getArticals',function(req,res){
    const form = new multiparty.Form();
    const Item = require('./models/articals.js');
    form.parse(req , function(err,fields,files){
        var page = parseInt(fields.page);
        var pageSize = parseInt(fields.pageSize);
        var query = Item.find({});
        query.skip((page-1)*pageSize);
        query.limit(pageSize);
        query.exec(function(err,rs){
               if(err) return next(err);
               console.log(rs);
               res.json(rs);
           }) 
    })
})
app.listen(3000);

articals.js

var mongoose = require('mongoose');
//mongoose.connect('mongodb://localhost/hangaoke');
try {
    mongoose.connect('mongodb://localhost/hangaoke'); //- starting a db connection
}catch(err) {
    mongoose.createConnection('mongodb://localhost/hangaoke'); //- starting another db connection
}
var schema = new mongoose.Schema({
    title:String,
    content:String,
},{
    versionKey: false // You should be aware of the outcome after set to false
})
module.exports = mongoose.model('articals' , schema);
    原文作者:小脑fu
    原文地址: https://segmentfault.com/a/1190000008276824
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞